本文介紹了委派任務(wù)并在完成時(shí)收到通知(在 C# 中)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問題描述
從概念上講,我想完成以下操作,但在理解如何在 C# 中正確編碼時(shí)遇到了麻煩:
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;
}
然后,當(dāng) WorkerMethod() 完成時(shí),運(yùn)行:
Then, when WorkerMethod() is complete, run this:
void SomeOtherMethod() // Also member of AClass{}
{ ... }
誰(shuí)能舉個(gè)例子?
推薦答案
BackgroundWorker 類正是為此目的添加到 .NET 2.0 中的.
The BackgroundWorker class was added to .NET 2.0 for this exact purpose.
簡(jiǎn)而言之:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate { myBClass.DoHardWork(); }
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SomeOtherMethod);
worker.RunWorkerAsync();
如果需要,您還可以添加諸如取消和進(jìn)度報(bào)告之類的奇特內(nèi)容:)
You can also add fancy stuff like cancellation and progress reporting if you want :)
這篇關(guān)于委派任務(wù)并在完成時(shí)收到通知(在 C# 中)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!