我正在使用 aBackgroundWorker
来执行一些处理工作,同时在前台显示进度 basr。现在我知道我不应该访问其他线程使用的属性,因为这似乎会导致各种错误。但是在下面的例子中,我不认为我在做这样的事情,但是BackgroundWorker
当它到达ShowDialog()
方法时神奇地终止了:
public class ProcessingWindow
{
List<ProcessingJob> m_processingJobs = new List<ProcessingJob>();
private BackgroundWorker m_backGroundWorker = new BackgroundWorker();
public ProcessingWindow()
{
InitializeComponent();
/* Fill m_processingJobs with jobs */
m_backGroundWorker.WorkerReportsProgress = true;
m_backGroundWorker.WorkerSupportsCancellation = true;
m_backGroundWorker.DoWork += m_backgroundWorker_DoWork;
m_backGroundWorker.ProgressChanged += m_backgroundWorker_ProgressChanged;
m_backGroundWorker.RunWorkerCompleted += m_backgroundWorker_RunWorkerCompleted;
this.Loaded += ProcessingProgressWindow_Loaded;
}
void ProcessingWindow_Loaded(object sender, RoutedEventArgs e)
{
m_backGroundWorker.RunWorkerAsync();
}
private void m_backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
foreach (ProcessingJob job in m_processingJobs )
{
if (job.somethingIsWrong)
{
SomethingIsWrongDialog dialog = new SomethingIsWrongDialog(); // <-- Here it crashes!
// This statement is therefore never reached:
dialog.showDialog();
}
}
}
private void m_backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Debug.WriteLine("Finished!");
this.Close();
}
}
所以我的程序将命中 的构造函数SomethingIsWrongDialog
,然后,而不是崩溃,只是停止线程,并执行该m_backgroundWorker_RunWorkerCompleted
方法,就好像什么都没发生一样。为什么这是错误的?