Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Dado que no se espera esta llamada, la ejecución del método actual continúa antes de que se complete la llamada. Considere la posibilidad de aplicar el Await operador al resultado de la llamada.
El método actual llama a un método asincrónico que devuelve o TaskTask<TResult> y no aplica el operador Await al resultado. La llamada al método asincrónico inicia una tarea asincrónica. Sin embargo, dado que no se aplica ningún Await operador, el programa continúa sin esperar a que se complete la tarea. En la mayoría de los casos, ese comportamiento no se espera. Normalmente, otros aspectos del método de llamada dependen de los resultados de la llamada o, mínimamente, se espera que el método llamado se complete antes de volver del método que contiene la llamada.
Un problema igualmente importante es lo que sucede con excepciones que se generan en el método asincrónico llamado . Excepción que se genera en un método que devuelve o TaskTask<TResult> se almacena en la tarea devuelta. Si no espera la tarea o comprueba explícitamente si hay excepciones, se pierde la excepción. Si espera la tarea, se vuelve a iniciar su excepción.
Como procedimiento recomendado, siempre debe esperar la llamada.
De forma predeterminada, este mensaje es una advertencia. Para obtener más información sobre cómo ocultar advertencias o tratar advertencias como errores, vea Configurar advertencias en Visual Basic.
Identificador de error: BC42358
Para solucionar esta advertencia
Debe considerar la posibilidad de suprimir la advertencia solo si está seguro de que no desea esperar a que se complete la llamada asincrónica y que el método llamado no genere ninguna excepción. En ese caso, puede suprimir la advertencia asignando el resultado de la tarea de la llamada a una variable.
En el ejemplo siguiente se muestra cómo provocar la advertencia, cómo suprimirla y cómo esperar la 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 Llamar #1 o Llamar a #2, el método asincrónico no personalizado (CalledMethodAsync) finaliza después de que se completen tanto su llamador (CallingMethodAsync) como el autor de la llamada (StartButton_Click). La última línea de la salida siguiente muestra cuándo finaliza el método llamado. La entrada a y la salida del controlador de eventos que llama CallingMethodAsync al ejemplo completo se marcan en la salida.
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 Windows Presentation Foundation (WPF) contiene los métodos del ejemplo anterior. Los pasos siguientes configuran la aplicación:
Cree una aplicación WPF y asígnela
AsyncWarningel nombre .En el Editor de código de Visual Studio, elija la pestaña MainWindow.xaml .
Si la pestaña no está visible, abra el menú contextual de MainWindow.xaml en el Explorador de soluciones y elija Ver código.
Reemplace el código de la vista XAML de MainWindow.xaml por el código siguiente:
<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>Aparece una ventana simple que contiene un botón y un cuadro de texto en la vista Diseño de MainWindow.xaml.
Para obtener más información sobre el Diseñador XAML, consulta Crear una interfaz de usuario mediante el Diseñador XAML. Para obtener información sobre cómo crear su propia interfaz de usuario sencilla, vea las secciones "Para crear una aplicación WPF" y "Para diseñar un wpF MainWindow simple" de Tutorial: Acceso a la Web mediante Async y Await.
Reemplace el código de MainWindow.xaml.vb por 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.Presione la tecla F5 para ejecutar el programa y elija el botón Inicio .
La salida esperada aparece al final del código.