从概念上讲,我想完成以下工作,但是遇到麻烦,在C#中正确编写代码:
SomeMethod { // Member of AClass{} DoSomething; Start WorkerMethod() from BClass in another thread; DoSomethingElse; }
然后,当WorkerMethod()完成时,运行如下:
void SomeOtherMethod() // Also member of AClass{} { ... }
任何人都可以举个例子吗?
解决方法
为了确切的目的,将
BackgroundWorker类添加到.NET 2.0.
简单来说,你做:
BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate { myBClass.DoHardWork(); } worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SomeOtherMethod); worker.RunWorkerAsync();
您还可以添加喜欢的消息,如取消和进度报告,如果你想:)