Share via


Avertissement du compilateur (niveau 1) CS4014

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.Envisagez d'appliquer l'opérateur 'await' au résultat de l'appel.

La methode actuelle appelle une méthode d'opération asynchrone qui renvoie Task ou Task et n'applique pas l'opérateur attendez au résultat. L'appel de la méthode de l'opération asynchrone démarre une tâche asynchrone. Toutefois, étant donné qu'aucun opérateur await n'est appliqué, le programme se poursuit sans attendre la tâche. Dans la plupart des cas, n'est pas ce comportement que vous attendriez. En général les autres méthodes de la méthode appellante dépendent du résultat de l'appel ou, au moins, il est prévu que la méthode soit appelée avant que vous reveniez de la méthode qui contient l'appel.

Une question également importante est ce qui arrive aux exceptions qui sont générées dans la méthode appelée d'opération asynchrone. Une exception levée d'une méthode qui renvoie Task ou Task est stockée dans la tâche renvoyée. Si vous ne prévoyez pas la tâche ou qye vous ne cherchez pas explicitement les exceptions, l'exception est perdue. Si vous attendez la tâche, l'exception est renvoyée.

À titre de recommandation, vous devriez toujours attendre l'appel.

Envisagez de supprimer l'avertissement uniquement si vous êtes certain que vous ne souhaitez pas attendre l'appel asynchrone de l'exécution et que la méthode appelée ne génère aucune exception. Dans ce cas, vous pouvez supprimer l'avertissement en affectant le résultat de l'appel à une variable.

L'exemple suivant indique comment générer un avertissement, comment le supprimer, ainsi que l'appel.

async Task CallingMethodAsync()
{
    resultsTextBox.Text += "\r\n  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.
    var 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 warning CS4014.
    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, 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. 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 += "\r\n  Returning from calling method.";
}


async Task CalledMethodAsync(int howLong)
{
    resultsTextBox.Text += 
        "\r\n    Entering called method, starting and awaiting Task.Delay.";

    // Slow the process down a little so that you can distinguish between
    // awaiting and not awaiting in the program's output. Adjust the value
    // for howLong if necessary.
    await Task.Delay(howLong);
    resultsTextBox.Text += 
        "\r\n    Task.Delay is finished--returning from called method.";
}

Dans l'exemple, si vous choisissez l'appel #1 ou appelez #2, la méthode d'opération asynchrone inattendue (CalledMethodAsync) termine après que son appelant (CallingMethodAsync) et l'appelant de l'appelant (startButton_Click) soient terminées. La dernière ligne de la sortie suivante vous préviens lorque la méthode 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.

Vous pouvez également supprimer des avertissements du compilateur à l'aide des directives de #pragma warning (référence C#).

Exemple

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

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

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

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

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

    <Window x:Class="AsyncWarning.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 boutton et une zone de texte apparaît dans la vue Design de 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 manière de créer votre propre interface utilisateur simple, consultez « pour créer une application WPF » et « concevoir les sections de 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.cs par le code suivant.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace AsyncWarning
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private async void startButton_Click(object sender, RoutedEventArgs e)
            {
                resultsTextBox.Text += "\r\nEntering the Click event handler.";
                await CallingMethodAsync();
                resultsTextBox.Text += "\r\nExiting the Click event handler.";
            }
    
    
            async Task CallingMethodAsync()
            {
                resultsTextBox.Text += "\r\n  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.
                var 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 warning CS4014.
                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, 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. 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 += "\r\n  Returning from calling method.";
            }
    
    
            async Task CalledMethodAsync(int howLong)
            {
                resultsTextBox.Text += 
                    "\r\n    Entering called method, starting and awaiting Task.Delay.";
    
                // Slow the process down a little so that you can distinguish between
                // awaiting and not awaiting in the program's output. Adjust the value
                // for howLong if necessary.
                await Task.Delay(howLong);
                resultsTextBox.Text += 
                    "\r\n    Task.Delay is finished--returning from called method.";
            }
        }
    
        // Output with Call #1 or Call #2. (Wait for the last line to appear.)
    
        // 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 with Call #3, which awaits the call to CalledMethodAsync.
    
        // 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 attendue apparaît à la fin de code.

Voir aussi

Référence

await (Référence C#)

Concepts

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