Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Process.EnableRaisingEvents-Eigenschaft

 

Veröffentlicht: Oktober 2016

Ruft ab oder legt fest, ob beim Beenden des Prozesses das Exited-Ereignis ausgelöst werden soll.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

[BrowsableAttribute(false)]
public bool EnableRaisingEvents { get; set; }

Eigenschaftswert

Type: System.Boolean

true, wenn das Exited-Ereignis ausgelöst werden soll, wenn der zugeordnete Prozess beendet wird (entweder durch Beenden oder einen Aufruf von Kill); andernfalls false. Die Standardeinstellung ist false.

The P:System.Diagnostics.Process.EnableRaisingEvents property indicates whether the component should be notified when the operating system has shut down a process. The P:System.Diagnostics.Process.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 M:System.Diagnostics.Process.WaitForExit(System.Int32) method.

System_CAPS_noteHinweis

If you are using Visual Studio and double-click a T:System.Diagnostics.Process component in your project, an E:System.Diagnostics.Process.Exited event delegate and event handler are automatically generated. Additional code sets the P:System.Diagnostics.Process.EnableRaisingEvents property to false. You must change this property to true for your event handler to be executed when the associated process exits.

When an associated process exits after being shut down by the operating system either through a normal or abnormal termination, the operating system notifies each process component to which the process was associated, as long as the component's P:System.Diagnostics.Process.EnableRaisingEvents value is true. If a component started the process, the component can then access the administrative information for the associated process, which is still being stored by the operating system. Such information includes the P:System.Diagnostics.Process.ExitTime, and the P:System.Diagnostics.Process.ExitCode.

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

There is a cost associated with watching for a process to exit. If P:System.Diagnostics.Process.EnableRaisingEvents is true, the E:System.Diagnostics.Process.Exited event is raised when the associated process terminates. The procedures that you have specified for the E:System.Diagnostics.Process.Exited event run at that time.

Sometimes, your application starts a process but does not need to be notified 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 not be notified when the process exits, because it is not relevant to the continued operation of your application. Setting P:System.Diagnostics.Process.EnableRaisingEvents to false saves system resources.

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

using System;
using System.Diagnostics;
using System.Threading;

class PrintProcessClass
{

    private Process myProcess = new Process();
    private int elapsedTime;
    private bool eventHandled;

    // Print a file with any known extension.
    public void PrintDoc(string fileName)
    {

        elapsedTime = 0;
        eventHandled = false;

        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 \"{0}\":" + "\n" + ex.Message, fileName);
            return;
        }

        // Wait for Exited event, but not more than 30 seconds.
        const int SLEEP_AMOUNT = 100;
        while (!eventHandled)
        {
            elapsedTime += SLEEP_AMOUNT;
            if (elapsedTime > 30000)
            {
                break;
            }
            Thread.Sleep(SLEEP_AMOUNT);
        }
    }

    // Handle Exited event and display process information.
    private void myProcess_Exited(object sender, System.EventArgs e)
    {

        eventHandled = true;
        Console.WriteLine("Exit time:    {0}\r\n" +
            "Exit code:    {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, elapsedTime);
    }

    public static void 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();
        myPrintProcess.PrintDoc(args[0]);
    }
}

LinkDemand

for full trust for the immediate caller. This member cannot be used by partially trusted code.

.NET Framework
Verfügbar seit 1.1
Zurück zum Anfang
Anzeigen: