我有一个应用程序,忙时会打开一个忙表单(FormWaitingForm)来向用户表明应用程序正忙。如何在事件backgroundWorker1_RunWorkerCompletedbelow中关闭FormWaitingForm?
private void radButtonCheckFiles_Click(object sender, EventArgs e)
{
var bw = new BackgroundWorker();
// define the event handlers
bw.DoWork += new DoWorkEventHandler(ProcessTickTemp);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
bw.RunWorkerAsync(); // starts the background worker
// execution continues here in parallel to the background worker
using (var FormWaitingForm = new WaitingForm()) //
{
var result = FormWaitingForm.ShowDialog();
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// How do i close FormWaitingForm here ?
}
答案 0 :(得分:2)
你可以尝试这样的事情。保留对click方法之外的表单的引用,然后以非模态方式打开它(这样您就不必等待用户关闭它)。
WaitingForm formWaitingForm;
private void radButtonCheckFiles_Click(object sender, EventArgs e)
{
// background code here
formWaitingForm = new WaitingForm();
formWaitingForm.Show();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
formWaitingForm.Close();
formWaitingForm.Dispose();
}
如果用户关闭等待表单而不等待您这样做,则必须添加一些代码来处理。
也就是说,我通常实现等待/进度类型表单的方式是将后台进程合并到进度表单本身并显示类似进度条的内容。
This link可能会给你一些更多的想法。