Compartir a través de


Como esta llamada no es "awaited", la ejecución del método actual continuará antes de que se complete la llamada.

Mensaje de error

Como esta llamada no es 'awaited', la ejecución del método actual continuará antes de que se complete la llamada.Considere la posibilidad de aplicar el operador de “Await” al resultado de la llamada.

Las llamadas al método actuales un método async que devuelve Task o Task y no aplica el operador de Espera al resultado. La llamada al método async inicia una tarea asincrónica. Sin embargo, porque no se aplica ningún operador de Await , el programa continúa sin esperar a que la tarea se complete. En la mayoría de los casos, ese comportamiento no se espera. Otros aspectos del método de llamada dependen normalmente de los resultados de la llamada o, como mínimo, se espera que el método denominado se completa antes de volver del método que contiene la llamada.

Un problema igualmente importante sucede con las excepciones que se producen en el método denominado async. Una excepción que se produce en un método que devuelve Task o Task se almacena en la tarea devuelta. Si no espera la tarea o explícitamente la comprobación de excepciones, se pierde la excepción. Si se espera la tarea, la excepción se reinician.

Como procedimiento recomendado, debería aguardar siempre la llamada.

De forma predeterminada, este mensaje es una advertencia. Para obtener más información sobre cómo ocultar las advertencias o tratarlas como errores, vea Configurar advertencias en Visual Basic.

Identificador de error: BC42358

Para resolver esta advertencia

  • Debería considerar suprimir la advertencia sólo si está seguro de que no desea esperar la llamada asincrónica finalice y que el método denominado no producirá ninguna excepción. En ese caso, puede suprimir la advertencia asignando el resultado de la tarea de la llamada a una variable.

    El ejemplo siguiente se muestra cómo provocar la advertencia, cómo suprimirla, y cómo aguardar llamada.

        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
    

    En el ejemplo, si elige la llamada n.º o llama #2, complete los finals unawaited del método async (CalledMethodAsync) detrás del llamador (CallingMethodAsync) y el llamador del llamador (StartButton_Click). La última línea en el resultado siguiente muestra cuando los finals de método. Marca la entrada a y la salida del controlador de eventos que llama a CallingMethodAsync en el ejemplo completo en el resultado.

    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.
    

Ejemplo

La siguiente aplicación de (WPF) de Windows Presentation Foundation contiene los métodos del ejemplo anterior. Los pasos siguientes configurar la aplicación.

  1. Cree una aplicación WPF, y denomínela AsyncWarning.

  2. En el editor de código de Visual Studio, elija la pestaña de MainWindow.xaml .

    Si la pestaña no está visible, abra el menú contextual para MainWindow.xaml en Explorador de solucionesy, a continuación Ver código.

  3. Reemplace el código en la vista de XAML de MainWindow.xaml con el código siguiente.

    <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>
    

    Una ventana simple que contiene un botón y un cuadro de texto aparece en la vista de Diseño MainWindow.xaml.

    Para obtener más información sobre el XAML Designer, vea Tutorial: Crear una UI usando el Diseñador XAML. Para obtener información sobre cómo compilar su propia interfaz de usuario simple, vea “para crear una aplicación WPF” y “para diseñar las secciones de WPF un MainWindow simple” de Walkthrough: Acceso a web usando Async y Await (C# y Visual Basic).

  4. Reemplace el código en MainWindow.xaml.vb con el código siguiente.

    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. Elija la tecla F5 para ejecutar el programa, y elija el botón de Iniciar .

    La salida esperada aparece al final del código.

Vea también

Referencia

Await (Operador) (Visual Basic)

Conceptos

Programación asincrónica con Async y Await (C# y Visual Basic)