Lire en anglais

Partager via


ConsoleCancelEventArgs.Cancel Propriété

Définition

Obtient ou définit une valeur qui indique si le fait d'appuyer simultanément sur la touche de modification Control et la touche de console (Ctrl+C) ou la touche Ctrl + Saut C met fin au processus actuel. La valeur par défaut est false, qui met fin au processus actuel.

C#
public bool Cancel { get; set; }

Valeur de propriété

true si le processus actuel doit se poursuivre lorsque le gestionnaire d'événements prend fin ; false si le processus actuel doit prendre fin. La valeur par défaut est false. Le processus actuel se termine lorsque le gestionnaire d'événements est retourné. Si true, le processus actuel continue.

Exemples

L’exemple suivant montre comment utiliser la Cancel propriété pour gérer un événement.

C#
using System;

class Sample
{
    public static void Main()
    {
        ConsoleKeyInfo cki;

        Console.Clear();

        // Establish an event handler to process key press events.
        Console.CancelKeyPress += new ConsoleCancelEventHandler(myHandler);
        while (true)
        {
            Console.Write("Press any key, or 'X' to quit, or ");
            Console.WriteLine("CTRL+C to interrupt the read operation:");

            // Start a console read operation. Do not display the input.
            cki = Console.ReadKey(true);

            // Announce the name of the key that was pressed .
            Console.WriteLine($"  Key pressed: {cki.Key}\n");

            // Exit if the user pressed the 'X' key.
            if (cki.Key == ConsoleKey.X) break;
        }
    }

    protected static void myHandler(object sender, ConsoleCancelEventArgs args)
    {
        Console.WriteLine("\nThe read operation has been interrupted.");

        Console.WriteLine($"  Key pressed: {args.SpecialKey}");

        Console.WriteLine($"  Cancel property: {args.Cancel}");

        // Set the Cancel property to true to prevent the process from terminating.
        Console.WriteLine("Setting the Cancel property to true...");
        args.Cancel = true;

        // Announce the new value of the Cancel property.
        Console.WriteLine($"  Cancel property: {args.Cancel}");
        Console.WriteLine("The read operation will resume...\n");
    }
}
// The example displays output similar to the following:
//    Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//      Key pressed: J
//
//    Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//      Key pressed: Enter
//
//    Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//
//    The read operation has been interrupted.
//      Key pressed: ControlC
//      Cancel property: False
//    Setting the Cancel property to true...
//      Cancel property: True
//    The read operation will resume...
//
//      Key pressed: Q
//
//    Press any key, or 'X' to quit, or CTRL+C to interrupt the read operation:
//      Key pressed: X

Remarques

La Cancel propriété est automatiquement initialisée à false lorsque le gestionnaire de l’événement CancelKeyPress est appelé. La valeur de la Cancel propriété lorsque le gestionnaire d’événements se termine détermine si le processus en cours reprend ou s’arrête.

Dans une opération de jeu après l’appui sur Ctrl+C, spécifiez true pour indiquer que le processus en cours doit reprendre à la fin du gestionnaire d’événements, ou false pour indiquer que le processus en cours doit se terminer.

Notes pour les appelants

Dans .NET Framework 3.5 et .NET Framework 4, la tentative de définition de la propriété sur true si l’événement CancelCancelKeyPress a été appelé par l’utilisateur appuyant sur Ctrl+Arrêt a levé une InvalidOperationException exception. Dans .NET Framework 4.5, vous pouvez définir la Cancel propriété sur true après que l’utilisateur appuie sur Ctrl+Arrêt et annule l’arrêt de l’application.

S’applique à

Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

Voir aussi