Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Process.EnableRaisingEvents Property

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

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

public bool EnableRaisingEvents { get; set; }
/** @property */
public boolean get_EnableRaisingEvents ()

/** @property */
public void set_EnableRaisingEvents (boolean value)

public function get EnableRaisingEvents () : boolean

public function set EnableRaisingEvents (value : boolean)

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.

The EnableRaisingEvents property indicates 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.

NoteNote

If you are 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 be executed when the associated process exits.

When an associated process exits after being shut down by the operation 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 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 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 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 Process components, so it keeps the ExitTime and Handle information in memory.

There is a cost associated with watching for a process to exit. If EnableRaisingEvents is true, the Exited event is raised when the associated process terminates. The procedures that you have specified for the 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 EnableRaisingEvents to false saves system resources.

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.

Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Threading
Imports Microsoft.VisualBasic

Class PrintProcessClass

    Private WithEvents myProcess As New Process
    Private elapsedTime As Integer
    Private eventHandled As Boolean

    ' Print a file with any known extension.
    Sub PrintDoc(ByVal fileName As String)

        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.Start()

        Catch ex As Exception
            Console.WriteLine("An error occurred trying to print ""{0}"":" & _
                vbCrLf & ex.Message, fileName)
            Return
        End Try

        ' Wait for Exited event, but not more than 30 seconds.
        Const SLEEP_AMOUNT As Integer = 100
        Do While Not eventHandled
            elapsedTime += SLEEP_AMOUNT
            If elapsedTime > 30000 Then
                Exit Do
            End If
            Thread.Sleep(SLEEP_AMOUNT)
        Loop
    End Sub

    ' Handle Exited event and display process information.
    Private Sub myProcess_Exited(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles myProcess.Exited

        eventHandled = True
        Console.WriteLine("Exit time:    {0}" & vbCrLf & _
            "Exit code:    {1}" & vbCrLf & "Elapsed time: {2}", _
            myProcess.ExitTime, myProcess.ExitCode, elapsedTime)
    End Sub

    Shared Sub Main(ByVal args() As String)

        ' Verify that an argument has been entered.
        If args.Length <= 0 Then
            Console.WriteLine("Enter a file name.")
            Return
        End If

        ' Create the process and print the document.
        Dim myProcess As New PrintProcessClass
        myProcess.PrintDoc(args(0))
    End Sub
End Class

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.