每5秒执行一次按钮点击,直到后台worker停止运行并关闭
Execute button click every 5 seconds until background worker stops to operate and closes
我创建了一个调用 SQL table 的按钮,用户可以查看 table 的行。基本上,像
这样的查询
SELECT * FROM table1
按钮的名称为 View
。
此外,我还创建了以下后台工作程序,它执行 4 分钟的长 运行 查询:
using System.Windows;
using System.ComponentModel;
using System.Threading;
using System;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Input;
namespace TestEnvironment
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class ProgressBarTemplate : Window
{
private CreateProjectScreen _CreateProjectScreen;
private LoginScreen _LoginScreen;
public ProgressBarTemplate()
{
InitializeComponent();
}
public static int RunCalculationsMethod(string connectionstring, string foldername)
{
bool exists = Directory.Exists(foldername);
if (!exists)
{
Directory.CreateDirectory(foldername);
}
try
{
using (SqlConnection sqlConnection = new SqlConnection(connectionstring))
{
var calculations_query = "SELECT * FROM table1");
using SqlCommand sqlCommand = new SqlCommand(calculations_query, sqlConnection);
sqlConnection.Open();
sqlCommand.CommandTimeout = 60 * 10;
int NumbderOfRecords = sqlCommand.ExecuteNonQuery();
return NumbderOfRecords;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return -100;
}
}
private void Window_ContentRendered(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
int IsSuccessful = RunCalculationsMethod("Server=localhost;Database=DB_Name;Integrated Security=SSPI", String.Format("C:\folder_path\"));
}
void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// This is called on the UI thread when the DoWork method completes
// so it's a good place to hide busy indicators, or put clean up code
try
{
this.Close();
MessageBox.Show("DQ Calculations completed successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
这里的琐碎情况是按钮 View
在我的 MainWindow
上,而上面的代码在名为 ExecuteBGWorker()
.[=25 的第二个 window 上=]
我想实现的是做一个时间触发事件,每5秒执行一次按钮View
,直到后台worker停止。当工作人员停止时,按钮 View
(属于 MainWindow
)也将停止被自动点击。
下面是我如何从 MainWindow
中调用 ExecuteBGWorker()
方法的代码:
private void View_Click(object sender, RoutedEventArgs e)
{
// The code to execute *View* button;
}
private void CalculationsButton_Click(object sender, RoutedEventArgs e)
{
try
{
Application.Current.Dispatcher.Invoke((Action)delegate
{
ExecuteBGWorker win_worker = new ExecuteBGWorker();
win_worker .Show();
});
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
当我单击“查看”按钮时
5 秒后再次单击 sql table 有更多记录要显示
我显然不知道如何实现它。因此,请感谢我为 post 这个问题所做的努力。我确实搜索过这个,我发现 DispatcherTimer 定时器功能就像 中那样。但是,我很长一段时间都不熟悉 C#,所以我不确定如何实现它并使计时器在 BG_Worker 完成后关闭。
您应该将执行查询的代码移动到 class 的方法中,并且只需使用计时器每五分钟调用一次此方法,例如:
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += (s, e) =>
{
//execute query
};
timer.Interval = 60000 * 5;
timer.Start();
我创建了一个调用 SQL table 的按钮,用户可以查看 table 的行。基本上,像
这样的查询SELECT * FROM table1
按钮的名称为 View
。
此外,我还创建了以下后台工作程序,它执行 4 分钟的长 运行 查询:
using System.Windows;
using System.ComponentModel;
using System.Threading;
using System;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Input;
namespace TestEnvironment
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class ProgressBarTemplate : Window
{
private CreateProjectScreen _CreateProjectScreen;
private LoginScreen _LoginScreen;
public ProgressBarTemplate()
{
InitializeComponent();
}
public static int RunCalculationsMethod(string connectionstring, string foldername)
{
bool exists = Directory.Exists(foldername);
if (!exists)
{
Directory.CreateDirectory(foldername);
}
try
{
using (SqlConnection sqlConnection = new SqlConnection(connectionstring))
{
var calculations_query = "SELECT * FROM table1");
using SqlCommand sqlCommand = new SqlCommand(calculations_query, sqlConnection);
sqlConnection.Open();
sqlCommand.CommandTimeout = 60 * 10;
int NumbderOfRecords = sqlCommand.ExecuteNonQuery();
return NumbderOfRecords;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return -100;
}
}
private void Window_ContentRendered(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
int IsSuccessful = RunCalculationsMethod("Server=localhost;Database=DB_Name;Integrated Security=SSPI", String.Format("C:\folder_path\"));
}
void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// This is called on the UI thread when the DoWork method completes
// so it's a good place to hide busy indicators, or put clean up code
try
{
this.Close();
MessageBox.Show("DQ Calculations completed successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
这里的琐碎情况是按钮 View
在我的 MainWindow
上,而上面的代码在名为 ExecuteBGWorker()
.[=25 的第二个 window 上=]
我想实现的是做一个时间触发事件,每5秒执行一次按钮View
,直到后台worker停止。当工作人员停止时,按钮 View
(属于 MainWindow
)也将停止被自动点击。
下面是我如何从 MainWindow
中调用 ExecuteBGWorker()
方法的代码:
private void View_Click(object sender, RoutedEventArgs e)
{
// The code to execute *View* button;
}
private void CalculationsButton_Click(object sender, RoutedEventArgs e)
{
try
{
Application.Current.Dispatcher.Invoke((Action)delegate
{
ExecuteBGWorker win_worker = new ExecuteBGWorker();
win_worker .Show();
});
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
当我单击“查看”按钮时
5 秒后再次单击 sql table 有更多记录要显示
我显然不知道如何实现它。因此,请感谢我为 post 这个问题所做的努力。我确实搜索过这个,我发现 DispatcherTimer 定时器功能就像
您应该将执行查询的代码移动到 class 的方法中,并且只需使用计时器每五分钟调用一次此方法,例如:
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += (s, e) =>
{
//execute query
};
timer.Interval = 60000 * 5;
timer.Start();