這是處理BackGroundWorker的正確方法嗎?我不確定在調用.Dispose()之前是否需要刪除事件。另外調用RunWorkerCompleted委託內的.Dispose()可以嗎?處理BackGroundWorker的正確方法
public void RunProcessAsync(DateTime dumpDate)
{
BackgroundWorker worker = new BackgroundWorker();
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync(dumpDate);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// Do Work here
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
worker.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.DoWork -= new DoWorkEventHandler(worker_DoWork);
worker.Dispose();
}
這是一個窗體上的後臺工作? – 2010-03-30 01:38:05
是的,儘管我以編程方式創建了BGW,而不是將它放在設計器的窗體中。如圖所示,當我想運行線程時,BGW被創建。這個想法是每次調用線程時創建一個不同的BGW,並在完成時處理它們。 – galford13x 2010-03-30 21:31:36