共用方式為


BC42358:由於此呼叫未等候,因此在呼叫完成之前,目前方法的執行會繼續執行

由於此呼叫未等候,因此在呼叫完成之前,目前方法的執行會繼續執行。 請考慮將 Await 運算子套用至呼叫的結果。

目前的方法會呼叫異步方法,這個方法會 Task 傳回 或 Task<TResult> ,而且不會將 Await 運算子套用至結果。 異步方法的呼叫會啟動異步工作。 不過,由於未 Await 套用運算符,因此程式會繼續執行,而不會等待工作完成。 在大部分情況下,此行為並非預期。 呼叫方法的其他層面通常取決於呼叫的結果,或者,在從包含呼叫的方法傳回之前,應該先完成呼叫的方法。

同樣重要的問題是,在呼叫異步方法中引發的例外狀況會發生什麼事。 傳回 TaskTask<TResult> 的方法所引發的例外狀況會儲存在傳回的工作中。 如果您未等候工作或明確檢查例外狀況,則會遺失例外狀況。 如果您等候工作,則會重新擲回其例外狀況。

最佳做法是,您應該一律等候呼叫。

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

錯誤標識碼: BC42358

若要解決此警告

只有當您確定不想等待異步呼叫完成,而且呼叫的方法不會引發任何例外狀況時,才應考慮隱藏警告。 在此情況下,您可以將呼叫的工作結果指派給變數,以隱藏警告。

下列範例示範如何引發警告、如何隱藏警告,以及如何等候呼叫:

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,則未喚醒的異步方法 () 會在呼叫端 (CalledMethodAsyncCallingMethodAsync) 和呼叫端的呼叫端 (StartButton_Click) 完成之後完成。 下列輸出中的最後一行會在呼叫的方法完成時顯示。 在完整範例中呼叫 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 Code 編輯器中,選擇 MainWindow.xaml 索引標籤

    如果看不到索引標籤,請在 [方案總管] 中開啟MainWindow.xaml的快捷方式功能表,然後選擇 [ 檢視程序代碼]。

  3. 將 MainWindow.xaml XAML 檢視中的程式代碼取代為下列程式代碼:

    <Window x:Class="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">
        <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的<若要建立 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 鍵以執行程式,然後選擇 [ 開始 ] 按鈕。

    預期的輸出會出現在程式代碼結尾。

另請參閱