Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Poiché questa chiamata non è attesa, l'esecuzione del metodo corrente continua prima del completamento della chiamata. Valutare la possibilità di applicare l'operatore Await al risultato della chiamata.
Il metodo corrente chiama un metodo asincrono che restituisce un Task oggetto o Task<TResult> e non applica l'operatore Await al risultato. La chiamata al metodo asincrono avvia un'attività asincrona. Tuttavia, poiché non viene applicato alcun Await operatore, il programma continua senza attendere il completamento dell'attività. Nella maggior parte dei casi, questo comportamento non è previsto. In genere altri aspetti del metodo chiamante dipendono dai risultati della chiamata o, al minimo, il metodo chiamato deve essere completato prima di restituire dal metodo che contiene la chiamata.
Un problema altrettanto importante è ciò che accade con le eccezioni generate nel metodo asincrono chiamato. Eccezione generata in un metodo che restituisce un Task oggetto o Task<TResult> viene archiviata nell'attività restituita. Se non si attende l'attività o si verifica in modo esplicito la presenza di eccezioni, l'eccezione viene persa. Se si attende l'attività, viene generata nuovamente l'eccezione.
Come procedura consigliata, è consigliabile attendere sempre la chiamata.
Per impostazione predefinita, questo messaggio è un avviso. Per altre informazioni su come nascondere gli avvisi o considerare gli avvisi come errori, vedere Configurazione degli avvisi in Visual Basic.
ID errore: BC42358
Per risolvere questo avviso
È consigliabile eliminare l'avviso solo se si è certi che non si vuole attendere il completamento della chiamata asincrona e che il metodo chiamato non genererà eccezioni. In tal caso, è possibile eliminare l'avviso assegnando il risultato dell'attività della chiamata a una variabile.
L'esempio seguente illustra come causare l'avviso, come eliminarlo e come attendere la chiamata:
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
Nell'esempio, se si sceglie Call #1 o Call #2, il metodo asincrono non atteso (CalledMethodAsync) termina dopo che il chiamante (CallingMethodAsync) e il chiamante () del chiamante (StartButton_Click) sono stati completati. L'ultima riga nell'output seguente mostra quando termina il metodo chiamato. L'ingresso e l'uscita dal gestore eventi che chiama CallingMethodAsync nell'esempio completo sono contrassegnati nell'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.
Esempio
L'applicazione Windows Presentation Foundation (WPF) seguente contiene i metodi dell'esempio precedente. La procedura seguente consente di configurare l'applicazione:
Creare un'applicazione WPF e denominarla
AsyncWarning.Nell'Editor di codice di Visual Studio scegliere la scheda MainWindow.xaml .
Se la scheda non è visibile, aprire il menu di scelta rapida per MainWindow.xaml in Esplora soluzioni e quindi scegliere Visualizza codice.
Sostituire il codice nella visualizzazione XAML di MainWindow.xaml con il codice seguente:
<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>Una finestra semplice che contiene un pulsante e una casella di testo viene visualizzata nella visualizzazione Progettazione di MainWindow.xaml.
Per altre informazioni sulla finestra di progettazione XAML, vedere Creazione di un'interfaccia utente tramite la finestra di progettazione XAML. Per informazioni su come creare un'interfaccia utente semplice, vedere le sezioni "To create a WPF application" (Per creare un'applicazione WPF) e le sezioni "To design a simple WPF MainWindow" di Walkthrough: Accessing the Web by Using Async and Await .For information about how to build your own simple UI ui, see the "To create a WPF application" and "To design a simple WPF MainWindow" (Per progettare una semplice istanza di WPF MainWindow) di Walkthrough: Accessing the Web by Using Async and Await (Per progettare una semplice istanza di WPF MainWindow) di Walkthrough: Accessing the Web by Using Async and
Sostituire il codice in MainWindow.xaml.vb con il codice seguente.
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.Scegliere il tasto F5 per eseguire il programma e quindi scegliere il pulsante Start .
L'output previsto viene visualizzato alla fine del codice.