Process.EnableRaisingEvents Property

Definition

Gets or sets whether the Exited event should be raised when the process terminates.

C#
public bool EnableRaisingEvents { get; set; }
C#
[System.ComponentModel.Browsable(false)]
public bool EnableRaisingEvents { get; set; }

Property Value

true if the Exited event should be raised when the associated process is terminated (through either an exit or a call to Kill()); otherwise, false. The default is false. Note that even if the value of EnableRaisingEvents is false, the Exited event will be raised by the HasExited property accessor, if it determines that the process has exited.

Attributes

Examples

The following code example creates a process that prints a file. It sets the EnableRaisingEvents property to cause the process to raise the Exited event when it exits. The Exited event handler displays process information.

C#
using System;
using System.Diagnostics;
using System.Threading.Tasks;

class PrintProcessClass
{
    private Process myProcess;
    private TaskCompletionSource<bool> eventHandled;

    // Print a file with any known extension.
    public async Task PrintDoc(string fileName)
    {
        eventHandled = new TaskCompletionSource<bool>();

        using (myProcess = new Process())
        {
            try
            {
                // Start a process to print a file and raise an event when done.
                myProcess.StartInfo.FileName = fileName;
                myProcess.StartInfo.Verb = "Print";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.EnableRaisingEvents = true;
                myProcess.Exited += new EventHandler(myProcess_Exited);
                myProcess.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred trying to print \"{fileName}\":\n{ex.Message}");
                return;
            }

            // Wait for Exited event, but not more than 30 seconds.
            await Task.WhenAny(eventHandled.Task,Task.Delay(30000));
        }
    }

    // Handle Exited event and display process information.
    private void myProcess_Exited(object sender, System.EventArgs e)
    {
        Console.WriteLine(
            $"Exit time    : {myProcess.ExitTime}\n" +
            $"Exit code    : {myProcess.ExitCode}\n" +
            $"Elapsed time : {Math.Round((myProcess.ExitTime - myProcess.StartTime).TotalMilliseconds)}");
        eventHandled.TrySetResult(true);
    }

    public static async Task Main(string[] args)
    {
        // Verify that an argument has been entered.
        if (args.Length <= 0)
        {
            Console.WriteLine("Enter a file name.");
            return;
        }

        // Create the process and print the document.
        PrintProcessClass myPrintProcess = new PrintProcessClass();
        await myPrintProcess.PrintDoc(args[0]);
    }
}

Remarks

The EnableRaisingEvents property suggests whether the component should be notified when the operating system has shut down a process. The EnableRaisingEvents property is used in asynchronous processing to notify your application that a process has exited. To force your application to synchronously wait for an exit event (which interrupts processing of the application until the exit event has occurred), use the WaitForExit method.

Note

If you're using Visual Studio and double-click a Process component in your project, an Exited event delegate and event handler are automatically generated. Additional code sets the EnableRaisingEvents property to false. You must change this property to true for your event handler to execute when the associated process exits.

If the component's EnableRaisingEvents value is true, or when EnableRaisingEvents is false and a HasExited check is invoked by the component, the component can access the administrative information for the associated process, which remains stored by the operating system. Such information includes the ExitTime and the ExitCode.

After the associated process exits, the Handle of the component no longer points to an existing process resource. Instead, it can only be used to access the operating system's information about the process resource. The operating system is aware that there are handles to exited processes that haven't been released by Process components, so it keeps the ExitTime and Handle information in memory.

There's a cost associated with watching for a process to exit. If EnableRaisingEvents is true, the Exited event is raised when the associated process terminates. Your procedures for the Exited event run at that time.

Sometimes, your application starts a process but doesn't require notification of its closure. For example, your application can start Notepad to allow the user to perform text editing but make no further use of the Notepad application. You can choose to avoid notification when the process exits because it's not relevant to the continued operation of your application. Setting EnableRaisingEvents to false can save system resources.

Applies to

Product 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 1.1, 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 2.0, 2.1

See also