我想同步调用后台工作程序.我希望执行代码在backgroundworker完成执行时结束.
我的BackgroundWorker代码在这里:
我的BackgroundWorker代码在这里:
{ BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += DoWork; worker.RunWorkerCompleted += RunWorkerCompleted; ... worker.RunWorkerAsync(); //wait for execution to end }
解决方法
如果您不希望代码异步执行,请不要将其放在BackgroundWorker中…
{ DoWork(); }
但是,如果您确实需要在BackgroundWorker中使用代码有一些不明原因,可以使用以下命令:
ManualResetEvent mre = new ManualResetEvent(false); BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += DoWork; worker.RunWorkerCompleted += (s,e) => { RunWorkerCompleted(s,e); mre.Set(); }; // ... worker.RunWorkerAsync(); mre.WaitOne();