Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
Como essa chamada não é aguardada, a execução do método atual continua antes que a chamada seja concluída. Considere aplicar o Await operador ao resultado da chamada.
O método atual chama um método assíncrono que retorna um Task ou um Task<TResult> e não aplica o operador Await ao resultado. A chamada para o método assíncrono inicia uma tarefa assíncrona. No entanto, como nenhum Await operador é aplicado, o programa continua sem aguardar a conclusão da tarefa. Na maioria dos casos, esse comportamento não é esperado. Normalmente, outros aspectos do método de chamada dependem dos resultados da chamada ou, minimamente, espera-se que o método chamado seja concluído antes de você retornar do método que contém a chamada.
Um problema igualmente importante é o que acontece com exceções que são geradas no método assíncrono chamado. Uma exceção gerada em um método que retorna um Task ou Task<TResult> é armazenado na tarefa retornada. Se você não aguardar a tarefa ou verificar explicitamente se há exceções, a exceção será perdida. Se você aguardar a tarefa, sua exceção será relançada.
Como prática recomendada, você deve sempre aguardar a chamada.
Por padrão, essa mensagem é um aviso. Para obter mais informações sobre como ocultar avisos ou tratar avisos como erros, consulte Configurando avisos no Visual Basic.
ID do erro: BC42358
Para resolver esse aviso
Você deve considerar suprimir o aviso somente se tiver certeza de que não deseja aguardar a conclusão da chamada assíncrona e se o método chamado não gerará exceções. Nesse caso, você pode suprimir o aviso atribuindo o resultado da tarefa da chamada a uma variável.
O exemplo a seguir mostra como causar o aviso, como suprimi-lo e como aguardar a chamada:
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
No exemplo, se você escolher a Chamada nº 1 ou a Chamada nº 2, o método assíncrono não desacordado (CalledMethodAsync) será concluído depois que o chamador (CallingMethodAsync) e o chamador do chamador (StartButton_Click) forem concluídos. A última linha na saída a seguir mostra quando o método chamado é concluído. A entrada e a saída do manipulador de eventos que chama CallingMethodAsync o exemplo completo são marcadas na saída.
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.
Exemplo
O aplicativo WPF (Windows Presentation Foundation) a seguir contém os métodos do exemplo anterior. As seguintes etapas configuram o aplicativo:
Crie um aplicativo WPF e nomeie-o
AsyncWarning.No Editor de Código do Visual Studio, escolha a guia MainWindow.xaml.
Se a guia não estiver visível, abra o menu de atalho para MainWindow.xaml no Gerenciador de Soluções e escolha Exibir Código.
Substitua o código na exibição XAML de MainWindow.xaml pelo seguinte código:
<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>Uma janela simples que contém um botão e uma caixa de texto aparece na exibição Design de MainWindow.xaml.
Para obter mais informações sobre o Designer XAML, consulte Criando uma interface do usuário usando o Designer XAML. Para obter informações sobre como criar sua própria interface do usuário simples, consulte as seções "Para criar um aplicativo WPF" e "Para criar um WPF MainWindow simples" de Passo a passo: acessando a Web usando Async e Await.
Substitua o código em MainWindow.xaml.vb pelo código a seguir.
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.Escolha a tecla F5 para executar o programa e escolha o botão Iniciar .
A saída esperada é exibida no final do código.