如何在不阻塞UI的情况下等待线程完成products.txt",false);上面说了方法是通过StartProcessWithProgress()方法在内部调用线程。我希望线程在执行代码logic-2行之前完成。同时,它不应该停止由frmProgressBar.UpdateProgress()完成的UI更新。我该怎么做呢?//代码逻辑-2MessageBox.Show("这正在立即执行。我想等到上面的线程完成");}}publicpartialclassfrmProgressBar:Form{publicvoidUpdateProgress(StringstrTextToDisplayOnProgress){progressBar1.BeginInvoke(newAction(()=>{progressBar1.Value++;lblFileName.Text=strTextToDisplayOnProgress;if(progressBar1.Value==progressBar1.Maximum){this.Hide();}}));}公共委托voidDelProgress();publicvoidStartProcessWithProgress(DelProgressdelMethodCode,intmaxCount){InitializeProgress(maxCount);ThreadbackgroundThread=newThread(newThreadStart(delMethodCode));backgroundThread.Start();}}publicstaticclassPullMSI{publicstaticfrmProgressBarExtractByMSIName(StringstrProductFilePath,boolreNameMSI){frmProgressBarfrmProgressBar=newfrmProgressBar();frmProgressBar.StartProcessWithProgress(()=>{//StreamRadersr声明和其他代码while(!sr.EndOfStream){//这里的逻辑frmProgressBar.UpdateProgress("Copyingsr.msiname");}},2);返回frmProgressBar;我很惊讶你以前没有使用过这些,但我真的建议你阅读C#中的线程,因为理解复杂性和学习语言对它很重要以下是非常重要的三个不同实现目标的方法:1.使用重置事件(进一步阅读:https://msdn.microsoft.com/en-us/library/system.threading.manualreseteventslim(v=vs.110).aspx)。如果您的C#版本没有ManualResetEventSlim,请将其替换为ManualResetEvent,并将Wait()更改为WaitOne()WaitOne()classLockingWithResetEvents{privatereadonlyManualResetEvent_resetEvent=newManualResetEvent(false);publicvoidTest(){MethodUsingResetEvents(;}privatevoidMethodUsingResetEvents(){ThreadPool.QueueUserWorkItem(_=>DoSomethingLong());ThreadPool.QueueUserWorkItem(_=>ShowMessageBox());}privatevoidDoSomethingLong(){Console.WriteLine("Doingsomthing.");Thread.Sleep(1000);_resetEvent.Set();}privatevoidShowMessageBox(){_resetEvent.WaitOne();Console.WriteLine("Helloworld.");}}2)使用任务并行库(TPL)。下一步阅读:https://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspxclassLockingWithTPL{publicvoidTest(){Task.Factory.StartNew(DoSomethingLong).ContinueWith(result=>ShowMessageBox());}privatevoidDoSomethingLong(){Console.WriteLine("Doingsomthing.");线程.睡眠(1000);}privatevoidShowMessageBox(){Console.WriteLine("Helloworld.");}}3)使用异步/等待。下一步阅读:https://msdn.microsoft.com/en-us/library/hh191443.aspxclassLockingWithAwait{publicvoidTest(){DoSomething();}privateasyncvoidDoSomething(){awaitTask.Run(()=>DoSomethingLong());显示消息框();}privateasyncvoidDoSomethingLong(){Console.WriteLine("Doingsomthing.");线程.睡眠(10000);}privatevoidShowMessageBox(){Console.WriteLine("Helloworld.");}}另外要了解:Mutex(https://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx),Semaphore(https://msdn.microsoft.com/en-us/library/system.threading.semaphore(v=vs.110).aspx),Lock(https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx),SemaphoreSlim(https://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim(v=vs.110).aspx),监控(https://msdn.microsoft.com/en-us/library/systemthreading.monitor(v=vs.110).aspx)和Interlocked(https://msdn.microsoft.com/en-us/library/system.threading.interlocked(v=vs.110).aspx)。如果您使用的是.NET4.0(使用VS2012)或更高版本,您可以使用任务并行库和异步等待轻松执行此操作:privateasyncvoidbutton1_Click(objectsender,EventArgse){Run(()=>PullMSI.ExtractByMSIName("products.txt",false));MessageBox.Show(string.Format("Returned{0}",frmProgressBarObj.ToString());}For.NET4你需要添加Microsoft.Bcl.Async以上是C#学习教程:Howtowaitforthethread至此分享完毕,不挡UI,如果对大家有用,需要了解更多C#学习教程,希望大家多加关注——本文收集自网络,不代表立场,如涉及侵权请点击维权联系管理员删除如转载请注明出处:
