本文介紹了委派任務并在完成時收到通知(在 C# 中)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
從概念上講,我想完成以下操作,但在理解如何在 C# 中正確編碼時遇到了麻煩:
Conceptually, I would like to accomplish the following but have had trouble understand how to code it properly in C#:
SomeMethod { // Member of AClass{}
DoSomething;
Start WorkerMethod() from BClass in another thread;
DoSomethingElse;
}
然后,當 WorkerMethod() 完成時,運行:
Then, when WorkerMethod() is complete, run this:
void SomeOtherMethod() // Also member of AClass{}
{ ... }
誰能舉個例子?
推薦答案
BackgroundWorker 類正是為此目的添加到 .NET 2.0 中的.
The BackgroundWorker class was added to .NET 2.0 for this exact purpose.
簡而言之:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate { myBClass.DoHardWork(); }
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SomeOtherMethod);
worker.RunWorkerAsync();
如果需要,您還可以添加諸如取消和進度報告之類的奇特內容:)
You can also add fancy stuff like cancellation and progress reporting if you want :)
這篇關于委派任務并在完成時收到通知(在 C# 中)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!