<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 編譯器警告(等級 1)CS4014 由于此調用不會等待,因此在調用完成前將繼續執行當前方法。請考慮將“await”運算符應用于調用結果。 當前方法調用返回 [Task](https://msdn.microsoft.com/zh-CN/library/system.threading.tasks.task.aspx) 或 [Task&lt;TResult&gt;](https://msdn.microsoft.com/zh-CN/library/dd321424.aspx),而不應用運算符 [等候](https://msdn.microsoft.com/zh-CN/library/hh156528.aspx) 到結果的異步方法。異步方法的調用啟動異步任務。但是,因為 **await** 運算符沒有應用,程序繼續運行,而不必等待任務完成。在大多數情況下,此行為不是您所期望的。通常調用方法的其他特性取決于調用的結果,或最低限度上,調用方法的預計在從包含調用的方法返回之前完成。 同樣至關重要的問題是被調用的異步方法中引發的異常發生了什么。在返回[Task](https://msdn.microsoft.com/zh-CN/library/system.threading.tasks.task.aspx)或 [Task&lt;TResult&gt;](https://msdn.microsoft.com/zh-CN/library/dd321424.aspx)方法中引發的異常被存儲在返回的任務中。如果不等待任務還不顯式檢查異常,該異常會丟失。如果在等待任務,異常會被重新拋出。 作為最佳做法,應始終在等待調用。 應考慮禁止顯示警告,僅當您確定不希望等待異步調用完成并且這個被調用的方法不會引發任何異常。在此情況下,可以通過分配調用的任務結果給變量來禁止警告。 下面的示例演示如何生成警告,如何顯示它以及如何等待調用。 ``` async Task CallingMethodAsync() { resultsTextBox.Text += "\r\n Entering calling method."; // Variable delay is used to slow down the called method so that you can // distinguish between awaiting and not awaiting in the program's output. // You can adjust the value to produce the output that this topic shows // after the code. var delay = 5000; // Call #1. // Call an async method. Because you don't await it, its completion // isn't coordinated with the current method, CallingMethodAsync. // The following line causes warning CS4014. CalledMethodAsync(delay); // Call #2. // To suppress the warning without awaiting, you can assign the // returned task to a variable. The assignment doesn't change how // the program runs. However, recommended practice is always to // await a call to an async method. // Replace Call #1 with the following line. //Task delayTask = CalledMethodAsync(delay); // Call #3 // To contrast with an awaited call, replace the unawaited call // (Call #1 or Call #2) with the following awaited call. Best // practice is to await the call. //await CalledMethodAsync(delay); // If the call to CalledMethodAsync isn't awaited, CallingMethodAsync // continues to run and, in this example, finishes its work and returns // to its caller. resultsTextBox.Text += "\r\n Returning from calling method."; } async Task CalledMethodAsync(int howLong) { resultsTextBox.Text += "\r\n Entering called method, starting and awaiting Task.Delay."; // Slow the process down a little so that you can distinguish between // awaiting and not awaiting in the program's output. Adjust the value // for howLong if necessary. await Task.Delay(howLong); resultsTextBox.Text += "\r\n Task.Delay is finished--returning from called method."; } ``` 在此示例中,如果您調用Call #1或Call #2,在調用方 (CallingMethodAsync) 和調用方的調用方 (startButton_Click) 完成后,不等待異步方法 (CalledMethodAsync) 。當調用的方法完成時,以下輸出的最后一行向你演示了。從在完整的示例中調用 CallingMethodAsync 的事件處理程序,為輸入和輸出做標記。 ``` Entering the Click event handler. Entering calling method. Entering called method, starting and awaiting Task.Delay. Returning from calling method. Exiting the Click event handler. Task.Delay is finished--returning from called method. ``` 使用 [#pragma warning(C# 參考)](https://msdn.microsoft.com/zh-CN/library/441722ys.aspx) 指令,還可以取消編譯器警告。 以下 Windows Presentation Foundation (WPF) 應用程序包含從前面的方法。下列步驟建立應用程序。 1. 創建 WPF 應用程序,將其命名為 **AsyncWarning**。 2. 在 Visual Studio 代碼編輯器中,選擇“MainWindow.xaml”選項卡。 如果此選項卡不可視,則在“解決方案資源管理器”中,打開 MainWindow.xaml 的快捷菜單,然后選擇“查看代碼”。 3. 在 MainWindow.xaml 的“XAML”視圖中,使用下面的代碼替換代碼。 ``` &lt;Window x:Class="AsyncWarning.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Button x:Name="startButton" Content="Start" HorizontalAlignment="Left" Margin="214,28,0,0" VerticalAlignment="Top" Width="75" HorizontalContentAlignment="Center" FontWeight="Bold" FontFamily="Aharoni" Click="startButton_Click" /&gt; &lt;TextBox x:Name="resultsTextBox" Margin="0,80,0,0" TextWrapping="Wrap" FontFamily="Lucida Console"/&gt; &lt;/Grid&gt; &lt;/Window&gt; ``` 包含按鈕和文本框的簡單窗口顯示在 MainWindow.xaml 的“設計”窗口中。 有關 XAML設計器的更多信息,請參見[在 Visual Studio 中,使用 XAML 設計器創建 UI](https://msdn.microsoft.com/zh-CN/library/hh921077.aspx)。有關如何生成自己的簡單的 UI,請參見 [演練:使用 Async 和 Await 訪問 Web(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/hh300224.aspx)的“創建 WPF 應用程序”和“設計的簡單 WPF MainWindow”一節。 4. 將MainWindow.xaml.cs中的代碼替換為以下代碼。 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace AsyncWarning { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void startButton_Click(object sender, RoutedEventArgs e) { resultsTextBox.Text += "\r\nEntering the Click event handler."; await CallingMethodAsync(); resultsTextBox.Text += "\r\nExiting the Click event handler."; } async Task CallingMethodAsync() { resultsTextBox.Text += "\r\n Entering calling method."; // Variable delay is used to slow down the called method so that you can // distinguish between awaiting and not awaiting in the program's output. // You can adjust the value to produce the output that this topic shows // after the code. var delay = 5000; // Call #1. // Call an async method. Because you don't await it, its completion // isn't coordinated with the current method, CallingMethodAsync. // The following line causes warning CS4014. CalledMethodAsync(delay); // Call #2. // To suppress the warning without awaiting, you can assign the // returned task to a variable. The assignment doesn't change how // the program runs. However, recommended practice is always to // await a call to an async method. // Replace Call #1 with the following line. //Task delayTask = CalledMethodAsync(delay); // Call #3 // To contrast with an awaited call, replace the unawaited call // (Call #1 or Call #2) with the following awaited call. Best // practice is to await the call. //await CalledMethodAsync(delay); // If the call to CalledMethodAsync isn't awaited, CallingMethodAsync // continues to run and, in this example, finishes its work and returns // to its caller. resultsTextBox.Text += "\r\n Returning from calling method."; } async Task CalledMethodAsync(int howLong) { resultsTextBox.Text += "\r\n Entering called method, starting and awaiting Task.Delay."; // Slow the process down a little so that you can distinguish between // awaiting and not awaiting in the program's output. Adjust the value // for howLong if necessary. await Task.Delay(howLong); resultsTextBox.Text += "\r\n Task.Delay is finished--returning from called method."; } } // Output with Call #1 or Call #2\. (Wait for the last line to appear.) // Entering the Click event handler. // Entering calling method. // Entering called method, starting and awaiting Task.Delay. // Returning from calling method. // Exiting the Click event handler. // Task.Delay is finished--returning from called method. // Output with Call #3, which awaits the call to CalledMethodAsync. // Entering the Click event handler. // Entering calling method. // Entering called method, starting and awaiting Task.Delay. // Task.Delay is finished--returning from called method. // Returning from calling method. // Exiting the Click event handler. } ``` 5. 選擇 F5 鍵運行程序,然后選擇“開始”按鈕。 預期的輸出顯示在代碼結尾。 ## 請參閱 [await(C# 參考)](https://msdn.microsoft.com/zh-CN/library/hh156528.aspx) [使用 Async 和 Await 的異步編程(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/hh191443.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看