一文说通C#中的异步编程
<div id="output_wrapper_id" class="output_wrapper" style="font-size: 16px; color: rgba(62, 62, 62, 1); line-height: 1.6; word-spacing: 0; letter-spacing: 0; font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif"><blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0; color: rgba(129, 145, 152, 1); border-left: 6px solid rgba(220, 230, 240, 1); background-color: rgba(242, 247, 251, 1); overflow: auto; word-wrap: normal; word-break: normal">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 0">天天写,不一定就明白。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 0">又及,前两天看了一个关于同步方法中调用异步方法的文章,里面有些概念不太正确,所以整理了这个文章。</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<h1 id="h" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">一、同步和异步。</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">先说同步。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">同步概念大家都很熟悉。在异步概念出来之前,我们的代码都是按同步的方式写的。简单来说,就是程序严格按照代码的逻辑次序,一行一行执行。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看一段代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">void</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>[] args)</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Syc proccess - start"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Syc proccess - enter Func1"</span>);<br> func1();<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Syc proccess - out Func1"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Syc proccess - enter Func2"</span>);<br> func2();<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Syc proccess - out Func2"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Syc proccess - enter Func3"</span>);<br> func3();<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Syc proccess - out Func3"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Syc proccess - done"</span>);<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">void</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func1</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - start"</span>);<br> Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1000</span>);<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - end"</span>);<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">void</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func2 proccess - start"</span>);<br> Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">3000</span>);<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func2 proccess - end"</span>);<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">void</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func3</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func3 proccess - start"</span>);<br> Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5000</span>);<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func3 proccess - end"</span>);<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这是一段简单的通常意义上的代码,程序按代码的次序同步执行,看结果:</p>
<pre><code class="hljs sql" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">Syc proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Syc proccess - enter Func1<br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Syc proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func1<br>Syc proccess - enter Func2<br>Func2 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func2 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Syc proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func2<br>Syc proccess - enter Func3<br>Func3 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func3 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Syc proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func3<br>Syc proccess - done<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">没有任何意外。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic"> <span style="font-size: small; color: inherit; line-height: inherit; margin: 0; padding: 0">为了防止不提供原网址的转载,特在这里加上原文链接:https://www.cnblogs.com/tiger-wang/p/13357981.html</span></em></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">那异步呢?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">异步,来自于对同步处理的改良和优化。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">应用中,经常会有对于文件或网络、数据库的IO操作。这些操作因为IO软硬件的原因,需要消耗很多时间,但通常情况下CPU计算量并不大。在同步的代码中,这个过程会被阻塞。直白的说法就是这一行代码没执行完成,程序就得等着,等完成后再执行下一行代码,而这个等待的时间中,CPU资源就被浪费了,闲着了,什么也没做。(当然,操作系统会调度CPU干别的,这儿不抬杠。)</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">异步编程模型和规范因此出现了,通过某种机制,让程序在等着IO的过程中,继续做点别的事,等IO的过程完成了,再回来处理IO的内容。这样CPU也没闲着,在等IO的过程中多做了点事。反映到用户端,就感觉程序更快了,用时更短了。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">下面重点说一下异步编程相关的内容。</p>
<h1 id="h-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">二、异步编程</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">C#中,异步编程,一个核心,两个关键字。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">一个核心是指<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task</code>和<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task<T></code>对象,而两个关键字,就是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">async</code>和<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">从各种渠道给出的异步编程,都是下面的方式:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">function</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* your code here */</span><br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">然后调用的方式:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">function</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">是这样的吗?嗯,图样图森破~~~</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们来看代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>[] args)</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - start"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func1"</span>);<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func1</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func1"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func2"</span>);<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func2"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func3"</span>);<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func3</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func3"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - done"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Main proccess - done"</span>);<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func1</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - start"</span>);<br> Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1000</span>);<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - end"</span>);<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func2 proccess - start"</span>);<br> Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">3000</span>);<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func2 proccess - end"</span>);<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func3</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func3 proccess - start"</span>);<br> Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5000</span>);<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func3 proccess - end"</span>);<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">跑一下结果:</p>
<pre><code class="hljs sql" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Async proccess - enter Func1<br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func1<br>Async proccess - enter Func2<br>Func2 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func2 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func2<br>Async proccess - enter Func3<br>Func3 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func3 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func3<br>Async proccess - done<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span> proccess - done<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">咦?这个好像跟同步代码的执行结果没什么区别啊?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">嗯,完全正确。上面这个代码,真的是同步执行的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这是异步编程的第一个容易错误的理解:<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">async</code>和<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>的配对。</p>
<h1 id="hasyncawait" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">三、async和await的配对</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在异步编程的规范中,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">async</code>修饰的方法,仅仅表示这个方法在内部<strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold">有可能</strong>采用异步的方式执行,CPU在执行这个方法时,会放到一个新的线程中执行。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">那这个方法,最终是否采用异步执行,<strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold">不决定于是否用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>方式调用这个方法,而决定于这个方法内部,是否有<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>方式的调用。</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看代码,很容易理解:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func1</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - start"</span>);<br> Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1000</span>);<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - end"</span>);<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个方法,因为方法内部没有<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>调用,所以这个方法永远会以同步方式执行,不管你调用这个方法时,有没有<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">而下面这个代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func1</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - start"</span>);<br> await Task.Run(() => Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1000</span>));<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - end"</span>);<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">因为这个方法里有<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>调用,所以这个方法不管你以什么方式调用,有没有<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>,都是异步执行的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>[] args)</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - start"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func1"</span>);<br> func1();<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func1"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func2"</span>);<br> func2();<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func2"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func3"</span>);<br> func3();<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func3"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - done"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Main proccess - done"</span>);<br><br> Console.ReadKey();<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func1</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - start"</span>);<br> await Task.Run(() => Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1000</span>));<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - end"</span>);<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func2 proccess - start"</span>);<br> await Task.Run(() => Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">3000</span>));<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func2 proccess - end"</span>);<br>}<br><br><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func3</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func3 proccess - start"</span>);<br> await Task.Run(() => Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5000</span>));<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func3 proccess - end"</span>);<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">输出结果:</p>
<pre><code class="hljs sql" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Async proccess - enter Func1<br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func1<br>Async proccess - enter Func2<br>Func2 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func2<br>Async proccess - enter Func3<br>Func3 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func3<br>Async proccess - done<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span> proccess - done<br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Func2 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Func3 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">结果中,在长时间运行<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Thread.Sleep</code>的时候,跳出去往下执行了,是异步。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">又有问题来了:不是说异步调用要用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>吗?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们把<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>加到调用方法的前边,试一下:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>[] args)</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - start"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func1"</span>);<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func1</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func1"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func2"</span>);<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func2"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func3"</span>);<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func3</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func3"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - done"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Main proccess - done"</span>);<br><br> Console.ReadKey();<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">跑一下结果:</p>
<pre><code class="hljs sql" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Async proccess - enter Func1<br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func1<br>Async proccess - enter Func2<br>Func2 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func2 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func2<br>Async proccess - enter Func3<br>Func3 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Func3 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func3<br>Async proccess - done<br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span> proccess - done<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">嗯?怎么又像是同步了?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">对,这是第二个容易错误的理解:<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>是什么意思?</p>
<h1 id="hawait" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">四、await是什么意思</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">提到<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>,就得先说说<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Wait</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">字面意思,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Wait</code>就是等待。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">前边说了,异步有一个核心,是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task</code>。而<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task</code>有一个方法,就是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Wait</code>,写法是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task.Wait()</code>。所以,很多人把这个<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Wait</code>和<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>混为一谈,<strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold">这是错的</strong>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个问题来自于<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task</code>。C#里,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task</code>不是专为异步准备的,它表达的是一个线程,是工作在线程池里的一个线程。异步是线程的一种应用,多线程也是线程的一种应用。<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Wait</code>,以及<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Status</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">IsCanceled</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">IsCompleted</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">IsFaulted</code>等等,是给多线程准备的方法,跟异步没有半毛钱关系。当然你非要在异步中使用多线程的<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Wait</code>或其它,从代码编译层面不会出错,但程序会。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">尤其,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task.Wait()</code>是一个同步方法,用于多线程中阻塞等待。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在那个「同步方法中调用异步方法」的文章中,用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task.Wait()</code>来实现同步方法中调用异步方法,这个用法本身就是错误的。 异步不是多线程,而且在多线程中,多个<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task.Wait()</code>使用也会死锁,也有解决和避免死锁的一整套方式。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">再说一遍:<strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Task.Wait()</code>是一个同步方法,用于多线程中阻塞等待,不是实现同步方法中调用异步方法的实现方式。</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">说回<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>。字面意思,也好像是等待。是真的吗?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">并不是,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>不完全是等待的意思。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在异步中,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>表达的意思是:当前线程/方法中,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>引导的方法出结果前,跳出当前线程/方法,从调用当前线程/方法的位置,去执行其它可能执行的线程/方法,并在引导的方法出结果后,把运行点拉回到当前位置继续执行;直到遇到下一个<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>,或线程/方法完成返回,跳回去刚才外部最后执行的位置继续执行。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">有点绕,还是看代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>[] args)</span><br> </span>{<br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1</span> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - start"</span>);<br><br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2</span> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func1"</span>);<br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">3</span> func1();<br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">4</span> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func1"</span>);<br><br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5</span> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - done"</span>);<br><br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">6</span> Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2000</span>);<br><br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">7</span> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Main proccess - done"</span>);<br><br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">8</span> Console.ReadKey();<br> }<br><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">func1</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br> </span>{<br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">9</span> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - start"</span>);<br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">10</span> await Task.Run(() => Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1000</span>));<br><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">11</span> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - end"</span>);<br> }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个代码,执行时是这样的:顺序执行1、2、3,进到<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">func1</code>,执行9、10,到10时,有<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>,所以跳出,执行4、5、6。而6是一个长时等待,在等待的过程中,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">func1</code>的10运行完成,运行点跳回10,执行11并结束方法,再回到6等待,结束等待后继续执行7、8结束。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们看一下结果:</p>
<pre><code class="hljs sql" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Async proccess - enter Func1<br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">start</span><br>Async proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">out</span> Func1<br>Async proccess - done<br>Func1 proccess - <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">end</span><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span> proccess - done<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">映证了这样的次序。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在这个例子中,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>在控制异步的执行次序。那为什么要用等待这么个词呢?是因为<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>确实有等待结果的含义。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>的第二层意思。</p>
<h1 id="hawait-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">五、await的第二层意思:等待拿到结果</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>确实有等待的含义。等什么?等异步的运行结果。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>[] args)</span><br></span>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - start"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - enter Func1"</span>);<br> Task<<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span>> f = func1();<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - out Func1"</span>);<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Async proccess - done"</span>);<br><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> result = await f;<br><br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Main proccess - done"</span>);<br><br> Console.ReadKey();<br>}<br><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task<<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span>> func1()<br>{<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - start"</span>);<br> await Task.Run(() => Thread.Sleep(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1000</span>));<br> Console.WriteLine(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Func1 proccess - end"</span>);<br><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">return</span> <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">5</span>;<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">比较一下这段代码和上一节的代码,很容易搞清楚执行过程。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个代码,完成了这样一个需求:我们需要使用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">func1</code>方法的返回值。我们可以提前去执行这个方法,而不急于拿到方法的返回值,直到我们需要使用时,再用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>去获取到这个返回值去使用。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这才是异步对于我们真正的用处。对于一些耗时的IO或类似的操作,我们可以提前调用,让程序可以利用执行过程中的空闲时间来完成这个操作。等到我们需要这个操作的结果用于后续的执行时,我们<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>这个结果。这时候,如果<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>的方法已经执行完成,那我们可以马上得到结果;如果没有完成,则程序将继续执行这个方法直到得到结果。</p>
<h1 id="h-2" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">六、同步方法中调用异步</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">正确的方法只有一个:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">func1().GetAwaiter().GetResult();<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这其实就是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">await</code>的一个变形。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">(全文完)</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<hr>
<p> </p>
<table border="0">
<tbody>
<tr>
<td><img src="https://img2020.cnblogs.com/blog/907112/202005/907112-20200527230728396-985375280.jpg"></td>
<td>
<p>微信公众号:老王Plus</p>
<p>扫描二维码,关注个人公众号,可以第一时间得到最新的个人文章和内容推送</p>
<p>本文版权归作者所有,转载请保留此声明和原文链接</p>
</td>
</tr>
</tbody>
</table>
</div><br><br>
来源:https://www.cnblogs.com/tiger-wang/p/13357981.html
頁:
[1]