Share via


Dans la mesure où cet appel n'est pas attendu, la méthode actuelle continue de s'exécuter avant la fin de l'appel.

Message d'erreur

Dans la mesure où cet appel n'est pas attendu, l'exécution de la méthode actuelle continue avant la fin de l'appel.Considérez appliquer « attendent » l'opérateur au résultat de l'appel.

Les appels de méthode actuels une méthode async qui retourne Task ou Task<TResult> et n'implémente pas l'opérateur d' attendez au résultat.L'appel à la méthode async lance une tâche asynchrone.Toutefois, car aucun opérateur d' Await n'est appliqué, le programme se poursuit sans attendre la tâche soit terminée.Dans la plupart des cas, ce comportement n'est pas prévu.Généralement d'autres aspects de la méthode d'appel dépendent des résultats de l'appel ou, de façon minimum, est censée la méthode appelée se termine avant de retourniez de la méthode qui contient l'appel.

Un problème également importante est ce qui se produit avec les exceptions déclenchées dans la méthode appelée async.Une exception qui est levée dans une méthode qui retourne Task ou Task<TResult> est stockée dans la tâche retournée.Si vous n'attendez pas la tâche ou ne vérifiez pas explicitement les exceptions, l'exception est perdue.Si vous attendez une tâche, l'exception est à nouveau levée.

Comme meilleure pratique, vous devez toujours attendre l'appel.

Par défaut, ce message est un avertissement.Pour plus d'informations sur le masquage des avertissements ou le traitement des avertissements en tant qu'erreurs, consultez Configuration d'avertissements en Visual Basic.

ID d'erreur : BC42358

Pour traiter cet avertissement

  • Vous devez envisager de supprimer l'avertissement uniquement si vous êtes certain que vous ne souhaitez pas attendre l'appel asynchrone se termine et que la méthode appelée ne lève pas d'exception.Dans ce cas, vous pouvez supprimer l'avertissement en assignant le résultat de la tâche de l'appel à une variable.

    L'exemple suivant montre comment générer l'avertissement, comment le supprimer, et comment attendre l'appel.

        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
    

    Dans l'exemple, si vous choisissez l'appel n°1 ou appelez n°2, la fin unawaited de méthode async (CalledMethodAsync) après son appelant (CallingMethodAsync) et l'appelant de l'appel (StartButton_Click) sont terminées.La dernière ligne de la sortie suivante indique une fois la méthode appelée se termine.L'entrée et la sortie du gestionnaire d'événements qui appelle CallingMethodAsync dans l'exemple complet sont marquées dans la sortie.

    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.
    

Exemple

L'application suivante Windows Presentation Foundation (WPF) contient les méthodes de l'exemple précédent.Les étapes suivantes ont installé l'application.

  1. Créer une application WPF, et nommez -la AsyncWarning.

  2. Dans l'éditeur de code Visual Studio, choisissez MainWindow.xaml tableau.

    Si l'onglet n'est pas visible, ouvrez le menu contextuel pour MainWindow.xaml dans Explorateur de solutions, puis choisissez Afficher le code.

  3. Remplacez le code dans la vue XAML MainWindow.xaml par le code suivant.

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

    Une fenêtre simple qui contient un bouton et une zone de texte apparaît dans la vue Design MainWindow.xaml.

    Pour plus d'informations sur le concepteur XAML, consultez Création d'une interface utilisateur à l'aide du concepteur XAML.Pour plus d'informations sur la génération de votre propre interface utilisateur simple, consultez « pour créer une application WPF » et « de concevoir des sections WPF un MainWindow simple » de Procédure pas à pas : accès au Web avec Async et Await (C# et Visual Basic).

  4. Remplacez le code dans MainWindow.xaml.vb par le code suivant.

    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. Choisissez la touche F5 pour exécuter le programme, puis choisissez le bouton Démarrer .

    La sortie appropriée apparaît à la fin de le code.

Voir aussi

Référence

Await, opérateur (Visual Basic)

Concepts

Programmation asynchrone avec Async et Await (C# et Visual Basic)