C# · 12月 20, 2021

C# — 等待异步操作执行完成的方式

C# — 等待异步操作执行完成的方式

1. 等待异步操作的完成,代码实现:

Main( mySum1 = mySum2 = mySum3 = AsyncCallback callback </span>= c => Console.WriteLine(<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;线程ID:{0},回调<a href=”https://www.jb51.cc/tag/hanshu/” target=”_blank” class=”keywords”>函数</a>执行:{1}</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,Thread.CurrentThread.ManagedThreadId,c.AsyncState); IAsyncResult result1</span>= mySum1.BeginInvoke(<span style=”color: #800080″&gt;10000</span>,callback,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>异步</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); IAsyncResult result2</span>= mySum2.BeginInvoke(<span style=”color: #800080″&gt;10000</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第二个异步</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); IAsyncResult result3</span>= mySum3.BeginInvoke(<span style=”color: #800080″&gt;10000</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第三个异步</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); </span><span style=”color: #008000″&gt;//</span><span style=”color: #008000″&gt;异步等待方式1:使用EndInvoke</span> <span style=”color: #0000ff”&gt;int</span> asyc1 =<span style=”color: #000000″&gt; mySum1.EndInvoke(result1); Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>异步已经执行完成,结果:{0}</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;,asyc1); </span><span style=”color: #008000″&gt;//</span><span style=”color: #008000″&gt;异步等待方式2:使用AsyncWaitHandle.WaitOne,参数-1表示一直等待到执行完成</span> <span style=”color: #0000ff”&gt;if</span> (result2.AsyncWaitHandle.WaitOne(-<span style=”color: #800080″&gt;1</span><span style=”color: #000000″&gt;)) { Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第二个异步执行完成</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); } </span><span style=”color: #008000″&gt;//</span><span style=”color: #008000″&gt;异步等待方式3:使用IsCompleted判断</span> <span style=”color: #0000ff”&gt;while</span> (!<span style=”color: #000000″&gt;result3.IsCompleted) { Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第三个异步还在执行中….</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); Thread.Sleep(</span><span style=”color: #800080″&gt;300</span><span style=”color: #000000″&gt;); } Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第三个异步执行完成</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); Console.ReadKey(); } </span><span style=”color: #0000ff”&gt;public</span> <span style=”color: #0000ff”&gt;static</span> <span style=”color: #0000ff”&gt;int</span> SumNumbers(<span style=”color: #0000ff”&gt;int</span><span style=”color: #000000″&gt; count) { </span><span style=”color: #0000ff”&gt;int</span> sum = <span style=”color: #800080″&gt;0</span><span style=”color: #000000″&gt;; </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> i = <span style=”color: #800080″&gt;0</span>; i < count; i++<span style=”color: #000000″&gt;) { sum </span>+=<span style=”color: #000000″&gt; i; } Thread.Sleep(</span><span style=”color: #800080″&gt;3000</span><span style=”color: #000000″&gt;); </span><span style=”color: #0000ff”&gt;return</span><span style=”color: #000000″&gt; sum; }}</span></pre>

2. 代码执行结果: