我有一个带有子窗口的 WPF 应用程序。在该应用程序中,我正在单击按钮(在后台工作人员中)进行数据库连接检查。该过程需要一些时间才能完成,在此期间,如果用户通过单击关闭按钮关闭子窗口,窗口会关闭,但后台工作人员会继续运行并在一段时间后显示消息。
这是示例代码:
BackgroundWorker worker;
private void Button_Click(object sender, RoutedEventArgs e)
{
worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
bsyInd.IsBusy = true;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
// checking database connectivity
string connstring=myconnstring;
SqlConnection con=new SqlConnection(connstring);
con.Open();
if(con.State==ConnectionState.Open)
e.Result=true;
}
catch (Exception)
{
e.Result=false;
}
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bool canConnect=(bool)e.Result;
if(canConnect)
MessageBox.Show("Connected");
else
MessageBox.Show("Failed");
bsyInd.IsBusy = false;
}
//close child window
private void ChildWindow_CloseButton_Click(object sender, RoutedEventArgs e)
{
//cancel the running process
worker.CancelAsync();
}
我在网上找到的所有解决方案都显示了在 Do_Work 内的循环中持续监视/轮询后台工作程序的 CancellationPending 属性的示例。由于我打算执行的过程不需要任何循环,因此如何监视子窗口关闭按钮单击事件的 CancellationPending 状态并取消后台进程?
提前致谢。