Export (0) Print
Expand All

Process.ExitCode Property

Gets the value that the associated process specified when it terminated.

[Visual Basic]
Public ReadOnly Property ExitCode As Integer
[C#]
public int ExitCode {get;}
[C++]
public: __property int get_ExitCode();
[JScript]
public function get ExitCode() : int;

Property Value

The code that the associated process specified when it terminated.

Exceptions

Exception Type Condition
InvalidOperationException The process has not exited.

-or-

The process Handle is not valid.

Remarks

Use ExitCode to get the status that the system process returned when it exited. You can use the exit code much like an integer return value from a main() procedure. How you choose to implement it is specific to your application.

Developers usually indicate a successful exit by an ExitCode value of zero, and designate errors by nonzero values that the calling method can use to identify the cause of an abnormal process termination. It is not necessary to follow these guidelines, but they are the convention.

If you try to get the ExitCode before the process has exited, the attempt throws an exception. Examine the HasExited property first to verify whether the associated process has terminated.

You can use the CloseMainWindow or the Kill method to cause an associated process to exit.

There are two ways of being notified when the associated process exits: synchronously and asynchronously. Synchronous notification relies on calling the WaitForExit method to pause the processing of your application until the associated component exits. Asynchronous notification relies on the Exited event. In either case, EnableRaisingEvents must be set to true for the Process component to receive notification that the process has exited.

Example

[Visual Basic, C#, C++] The following example starts an instance of Notepad. The example then retrieves and displays various properties of the associated process. The example detects when the process exits, and displays the process's exit code.

[Visual Basic] 
Imports System
Imports System.Diagnostics
Imports System.Threading

Namespace Process_Sample
   Class MyProcessClass

      Public Shared Sub Main()
         Try

            Dim myProcess As Process
            myProcess = Process.Start("NotePad.exe")

            While Not myProcess.HasExited

               Console.WriteLine()
               
               ' Get physical memory usage of the associated process.
               Console.WriteLine("Process's physical memory usage: " + _
                                      myProcess.WorkingSet.ToString)
               ' Get base priority of the associated process.
               Console.WriteLine("Base priority of the associated process: " + _
                                      myProcess.BasePriority.ToString)
               ' Get priority class of the associated process.
               Console.WriteLine("Priority class of the associated process: " + _
                                      myProcess.PriorityClass.ToString)
               ' Get user processor time for this process.
               Console.WriteLine("User Processor Time: " + _
                                      myProcess.UserProcessorTime.ToString)
               ' Get privileged processor time for this process.
               Console.WriteLine("Privileged Processor Time: " + _
                                   myProcess.PrivilegedProcessorTime.ToString)
               ' Get total processor time for this process.
               Console.WriteLine("Total Processor Time: " + _
                                     myProcess.TotalProcessorTime.ToString)
               ' Invoke overloaded ToString function.
               Console.WriteLine("Process's Name: " + myProcess.ToString)
               Console.WriteLine("-------------------------------------")

               If myProcess.Responding Then
                  Console.WriteLine("Status:  Responding to user interface")
                  myProcess.Refresh()
               Else
                  Console.WriteLine("Status:  Not Responding")
               End If
               Thread.Sleep(1000)
            End While

            Console.WriteLine()
            Console.WriteLine("Process exit code: {0}", myProcess.ExitCode)

         Catch e As Exception
            Console.WriteLine("The following exception was raised: " + e.Message)
         End Try
      End Sub 'Main
   End Class 'MyProcessClass
End Namespace 'Process_Sample


[C#] 
using System;
using System.Diagnostics;
using System.Threading;

namespace Process_Sample
{
   class MyProcessClass
   {
      public static void Main()
      {
         try
         {

            Process myProcess;
            myProcess = Process.Start("NotePad.exe");

            while(!myProcess.HasExited)
            {
               Console.WriteLine();

               // Get physical memory usage of the associated process.
               Console.WriteLine("Process's physical memory usage: " + myProcess.WorkingSet);
               // Get base priority of the associated process.
               Console.WriteLine("Base priority of the associated process: " + myProcess.BasePriority);
               // Get priority class of the associated process.
               Console.WriteLine("Priority class of the associated process: " + myProcess.PriorityClass);
               // Get user processor time for this process.
               Console.WriteLine("User Processor Time: " + myProcess.UserProcessorTime);
               // Get privileged processor time for this process.
               Console.WriteLine("Privileged Processor Time: " + myProcess.PrivilegedProcessorTime);
               // Get total processor time for this process.
               Console.WriteLine("Total Processor Time: " + myProcess.TotalProcessorTime);
               // Invoke overloaded ToString function.
               Console.WriteLine("Process's Name: " + myProcess.ToString());
               Console.WriteLine("-------------------------------------");

               if(myProcess.Responding)
               {
                  Console.WriteLine("Status:  Responding to user interface");
                  myProcess.Refresh();
               }
               else
               {
                  Console.WriteLine("Status:  Not Responding");
               }
               Thread.Sleep(1000);

            }

            Console.WriteLine();
            Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
         }
         catch(Exception e)
         {
            Console.WriteLine("The following exception was raised: " + e.Message);
         }
      }

   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;

int main() {
    try {
        Process* myProcess;
        myProcess = Process::Start(S"NotePad.exe");

        while(!myProcess->HasExited)       
        {
           Console::WriteLine();

            // Get physical memory usage of the associated process.
            Console::WriteLine(S"Process's physical memory usage: {0}", myProcess->WorkingSet.ToString());
            // Get base priority of the associated process.
            Console::WriteLine(S"Base priority of the associated process: {0}", myProcess->BasePriority.ToString());
            // Get priority class of the associated process.
            Console::WriteLine("Priority class of the associated process: {0}", __box(myProcess->PriorityClass));
            // Get user processor time for this process.
            Console::WriteLine(S"User Processor Time: {0}", myProcess->UserProcessorTime.ToString());
            // Get privileged processor time for this process.
            Console::WriteLine(S"Privileged Processor Time: {0}", myProcess->PrivilegedProcessorTime.ToString());
            // Get total processor time for this process.
            Console::WriteLine(S"Total Processor Time: {0}", myProcess->TotalProcessorTime.ToString());
            // Invoke overloaded ToString function.
            Console::WriteLine(S"Process's Name: {0}", myProcess->ToString());
            Console::WriteLine(S"-------------------------------------");

            if (myProcess->Responding) 
            {
                Console::WriteLine(S"Status:  Responding to user interface");
                myProcess->Refresh();
            } 
            else
            {
                Console::WriteLine(S"Status:  Not Responding");
            }

            Thread::Sleep(1000);
        }

        Console::WriteLine();
        Console::WriteLine("Process exit code: {0}", myProcess->ExitCode.ToString());

    } catch (Exception* e) {
       Console::WriteLine(S"The following exception was raised:  {0}", e->Message);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Process Class | Process Members | System.Diagnostics Namespace | HasExited | CloseMainWindow | Kill | WaitForExit | EnableRaisingEvents

Show:
© 2014 Microsoft