共用方式為


因為未等候此呼叫,所以在呼叫完成之前會繼續執行目前方法

錯誤訊息

因為未等候此呼叫,所以在呼叫完成之前會繼續執行目前方法。應用程式會呼叫的結果" 「Await運算子。

目前方法呼叫的傳回 TaskTask<TResult> ,不應用 等候 運算子套用至結果的非同步方法。 為非同步方法的呼叫開始非同步工作。 不過,在中,因為 Await 運算子不適用,程式會繼續執行,而不等候工作完成。 在大部分情況下,該行為沒有所需的。 通常會呼叫方法的其他方面相依於這個呼叫的結果,或至少,呼叫的方法必須完成,才能從包含呼叫的方法之前傳回。

一樣重要的問題是發生在呼叫的非同步方法引發的例外狀況。 在方法傳回 Task 引發或 Task<TResult> 的例外是這個傳回的工作中。 如果沒有等候工作也不會明確檢查例外狀況,例外狀況會遺失。 如果您正在等候該工作,其會重新擲回例外狀況。

基於最佳做法,您應該永遠等候呼叫。

根據預設,這是一個警告訊息。 如需隱藏警告或將警告視為錯誤的詳細資訊,請參閱在 Visual Basic 中設定警告

錯誤ID: B C42358

若要解決這個警告

  • 您應該考慮隱藏這個警告,只有在確定您不要等候非同步呼叫完成,呼叫的方法不會引發任何例外狀況。 在這種情況下,您可以指定呼叫的工作結果隱藏警告指派給變數。

    下列範例顯示如何產生這個警告,如何隱藏及如何等候呼叫。

        Async Function CallingMethodAsync() As Task
    
            ResultsTextBox.Text &= vbCrLf & "  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.
            Dim 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 the warning.
            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, the 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. The 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 &= vbCrLf & "  Returning from calling method."
        End Function
    
        Async Function CalledMethodAsync(howLong As Integer) As Task
    
            ResultsTextBox.Text &= vbCrLf & "    Entering called method, starting and awaiting Task.Delay."
            ' Slow the process down a little so you can distinguish between awaiting
            ' and not awaiting. Adjust the value for howLong if necessary.
            Await Task.Delay(howLong)
            ResultsTextBox.Text &= vbCrLf & "    Task.Delay is finished--returning from called method."
        End Function
    

    在此範例中,,您還是可以選擇呼叫#1或呼叫#2,在它的呼叫端(CallingMethodAsync)和呼叫端的呼叫端(StartButton_Click)之後unawaited非同步方法(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.
    

範例

下列Windows Presentation Foundation (WPF)應用程式包含前一個範例中的方法。 下列步驟會設定應用程式。

  1. 建立WPF應用程式,並將其命名為 AsyncWarning。

  2. 在Visual Studio程式碼編輯器中,選取 MainWindow.xaml 索引標籤。

    如果這個選項未顯示,請開啟MainWindow.xaml的捷徑功能表上 方案總管,然後選取 檢視程式碼

  3. 以下列程式碼取代MainWindow.xaml XAML 檢視的程式碼。

    <Window x:Class="MainWindow"
            xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <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" />
            <TextBox x:Name="ResultsTextBox" Margin="0,80,0,0" TextWrapping="Wrap" FontFamily="Lucida Console"/>
        </Grid>
    </Window>
    

    包含一個按鈕和一個文字方塊的簡單視窗會出現在MainWindow.xaml 設計 檢視。

    如需XAML設計工具的詳細資訊,請參閱 使用 XAML 設計工具建立 UI。 如需如何建置的資訊擁有簡單UI,請參閱 逐步解說:使用 Async 和 Await 存取 Web (C# 和 Visual Basic)「建立WPF應用程式」和「設計簡單的WPF MainWindow」一節。

  4. 以下列程式碼取代MainWindow.xaml.vb的程式碼。

    Class MainWindow 
    
        Private Async Sub StartButton_Click(sender As Object, e As RoutedEventArgs)
    
            ResultsTextBox.Text &= vbCrLf & "Entering the Click event handler."
            Await CallingMethodAsync()
            ResultsTextBox.Text &= vbCrLf & "Exiting the Click event handler."
        End Sub
    
    
        Async Function CallingMethodAsync() As Task
    
            ResultsTextBox.Text &= vbCrLf & "  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.
            Dim 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 the warning.
            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, the 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. The 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 &= vbCrLf & "  Returning from calling method."
        End Function
    
        Async Function CalledMethodAsync(howLong As Integer) As Task
    
            ResultsTextBox.Text &= vbCrLf & "    Entering called method, starting and awaiting Task.Delay."
            ' Slow the process down a little so you can distinguish between awaiting
            ' and not awaiting. Adjust the value for howLong if necessary.
            Await Task.Delay(howLong)
            ResultsTextBox.Text &= vbCrLf & "    Task.Delay is finished--returning from called method."
        End Function
    
    End Class
    
    ' Output
    
    ' 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
    
    ' 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 運算子 (Visual Basic)

概念

使用 Async 和 Await 設計非同步程式 (C# 和 Visual Basic)