Condividi tramite


Poiché la chiamata non può essere attesa, l'esecuzione del metodo corrente continua prima del completamento della chiamata

Messaggio di errore

Poiché la chiamata non può essere attesa, l'esecuzione del metodo corrente continua prima del completamento della chiamata.Applicare “attesa" operator il risultato della chiamata.

Le chiamate al metodo correnti un metodo asincrono che restituisce Task o Task<TResult> e non implementa l'operatore Attendere al risultato.La chiamata al metodo async avvia un'attività asincrona.Tuttavia, poiché alcun operatore Await viene applicato, il programma procederà senza attendere l'attività.Nella maggior parte dei casi, tale comportamento non è previsto.Generalmente altri aspetti del metodo chiamante dipendono dai risultati della chiamata a, come minimo, il metodo chiamato si prevede di completare prima del completamento del metodo che contiene la chiamata.

Un'edizione ugualmente importante è quello che accade con le eccezioni generate nel metodo chiamato async.Un'eccezione generata in un metodo che restituisce Task o Task<TResult> viene archiviata nell'attività.Se non si attendono l'attività o in modo esplicito il controllo per le eccezioni, l'eccezione viene persa.Se si attendono l'attività, la relativa generata di nuovo.

Come procedura consigliata, è necessario attendere sempre la chiamata.

Per impostazione predefinita, si tratta di un messaggio di avviso.Per ulteriori informazioni su come nascondere gli avvisi o considerarli come errori, vedere Configurazione degli avvisi in Visual Basic.

ID errore: BC42358

Se viene visualizzato questo avvertimento

  • Considerare la possibilità di eliminare l'avviso solo se si è certi che non si desidera attendere la chiamata asincrona per completare e che il metodo chiamato non generare alcuna eccezione.In tal caso, è possibile eliminare l'avviso assegnare il risultato di attività della chiamata a una variabile.

    Di seguito viene illustrato come generare 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 la chiamata n o chiamate n, il termine unawaited di metodo async (CalledMethodAsync) dopo il relativo chiamante (CallingMethodAsync) che il chiamante del chiamante (StartButton_Click) completi.L'ultima riga nell'output viene illustrato quando il metodo chiamato completa.La voce in e l'uscita dal gestore eventi che chiama CallingMethodAsync l'esempio completo sono contrassegnate 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

Nell'applicazione Windows Presentation Foundation (WPF) contiene i metodiesempio precedente.I passaggi seguenti si installa l'applicazione.

  1. Creazione di un'applicazione WPF e denominarla AsyncWarning.

  2. Nell'editor di codice di Visual Studio, scegliere la scheda MainWindow.xaml.

    Se la scheda non è visibile, scegliere dal menu di scelta rapida per MainWindow.xaml in Esplora soluzioniquindi scegliere Visualizza codice.

  3. Sostituire il codice nella visualizzazione XAML MainWindow.xaml con il codice seguente.

    <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 finestra semplice contenente un pulsante e una casella di testo visualizzato nella visualizzazione Progettazione MainWindow.xaml.

    Per ulteriori informazioni sulla finestra di progettazione XAML, vedere Creazione di un'interfaccia utente tramite XAML Designer.Per informazioni su come compilare una semplice interfaccia utente, vedere “per creare un'applicazione WPF" e “progettare le sezioni di una Finestra WPF semplice" Procedura dettagliata: accesso al Web tramite Async e Await (C# e Visual Basic).

  4. 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.
    
  5. Scegliere il tasto F5 per eseguire il programma e quindi scegliere il pulsante Avvia.

    L'output previsto viene visualizzato alla fine del codice.

Vedere anche

Riferimenti

Opertore Await (Visual Basic)

Concetti

Programmazione asincrona con Async e Await (C# e Visual Basic)