Process.ExitCode 属性

获取关联进程终止时指定的值。

**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)

语法

声明
Public ReadOnly Property ExitCode As Integer
用法
Dim instance As Process
Dim value As Integer

value = instance.ExitCode
public int ExitCode { get; }
public:
property int ExitCode {
    int get ();
}
/** @property */
public int get_ExitCode ()
public function get ExitCode () : int

属性值

关联进程在终止时指定的代码。

异常

异常类型 条件

InvalidOperationException

该进程尚未退出。

- 或 -

进程 Handle 无效。

备注

使用 ExitCode 获取系统进程在退出时返回的状态。使用退出代码与使用 main() 过程的整型返回值很相似。

进程的 ExitCode 值反映了应用程序开发人员为该进程实现的特定约定。如果要使用退出代码值在代码中作出决定,请确保了解应用程序进程使用的退出代码约定。

开发人员通常使用为零的 ExitCode 值指示成功退出,并通过非零值指出错误,调用方法可使用非零值来标识进程不正常终止的原因。虽然不必遵从这些准则,但这些准则是约定。

如果试图在进程退出前获取 ExitCode,则会引发异常。首先检查 HasExited 属性以验证关联的进程是否已终止。

可以使用 CloseMainWindowKill 方法使关联进程退出。

关联进程退出时有两种通知方式:同步和异步。同步通知依赖于调用 WaitForExit 方法以在关联组件退出前暂停应用程序的处理。异步通知依赖于 Exited 事件。任何一种情况下,EnableRaisingEvents 都必须设置为 true,以便 Process 组件接收进程退出的通知。

示例

下面的示例启动一个记事本实例。该示例然后检索并显示关联进程的各种属性。该示例检测该进程何时退出,并显示该进程的退出代码。

Imports System
Imports System.Diagnostics

Namespace ProcessSample
   Class ProcessMonitorSample

      Public Shared Sub Main()

         ' Define variables to track the peak
         ' memory usage of the process.
         Dim peakPagedMem As Long = 0
         Dim peakWorkingSet As Long = 0
         Dim peakVirtualMem As Long = 0

         Dim myProcess As Process = Nothing

         Try

            ' Start the process.
            myProcess = Process.Start("NotePad.exe")

            ' Display process statistics until
            ' the user closes the program.
            Do

                If Not myProcess.HasExited Then

                    ' Refresh the current process property values.
                    myProcess.Refresh()

                    Console.WriteLine()
                        
                    ' Display current process statistics.

                    Console.WriteLine("{0} -", myProcess.ToString())
                    Console.WriteLine("-------------------------------------")

                    Console.WriteLine("  physical memory usage: {0}", _
                         myProcess.WorkingSet64)
                    Console.WriteLine("  base priority: {0}", _
                         myProcess.BasePriority)
                    Console.WriteLine("  priority class: {0}", _
                         myProcess.PriorityClass)
                    Console.WriteLine("  user processor time: {0}", _
                         myProcess.UserProcessorTime)
                    Console.WriteLine("  privileged processor time: {0}", _
                         myProcess.PrivilegedProcessorTime)
                    Console.WriteLine("  total processor time: {0}", _
                         myProcess.TotalProcessorTime)

                    ' Update the values for the overall peak memory statistics.
                    peakPagedMem = myProcess.PeakPagedMemorySize64
                    peakVirtualMem = myProcess.PeakVirtualMemorySize64
                    peakWorkingSet = myProcess.PeakWorkingSet64

                    If myProcess.Responding Then
                        Console.WriteLine("Status = Running")
                    Else
                        Console.WriteLine("Status = Not Responding")
                    End If
                End If
            Loop While Not myProcess.WaitForExit(1000)

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

            ' Display peak memory statistics for the process.
            Console.WriteLine("Peak physical memory usage of the process: {0}", _
                peakWorkingSet)
            Console.WriteLine("Peak paged memory usage of the process: {0}", _
                peakPagedMem)
            Console.WriteLine("Peak virtual memory usage of the process: {0}", _
                peakVirtualMem)

         Finally
            If Not myProcess Is Nothing Then
                myProcess.Close
            End If
         End Try
      End Sub 'Main
   End Class 
End Namespace 
using System;
using System.Diagnostics;

namespace ProcessSample
{
    class ProcessMonitorSample
    {
        public static void Main()
        {

            // Define variables to track the peak
            // memory usage of the process.
            long peakPagedMem = 0, 
                peakWorkingSet = 0, 
                peakVirtualMem = 0;

            Process myProcess = null;

            try 
            {
                // Start the process.
                myProcess = Process.Start("NotePad.exe");
    
                // Display the process statistics until
                // the user closes the program.
                do 
                {
                    if (!myProcess.HasExited)
                    {
                        // Refresh the current process property values.
                        myProcess.Refresh();

                        Console.WriteLine();
                        
                        // Display current process statistics.

                        Console.WriteLine("{0} -", myProcess.ToString());
                        Console.WriteLine("-------------------------------------");

                        Console.WriteLine("  physical memory usage: {0}",
                            myProcess.WorkingSet64);
                        Console.WriteLine("  base priority: {0}",
                            myProcess.BasePriority);
                        Console.WriteLine("  priority class: {0}",
                            myProcess.PriorityClass);
                        Console.WriteLine("  user processor time: {0}",
                            myProcess.UserProcessorTime);
                        Console.WriteLine("  privileged processor time: {0}",
                            myProcess.PrivilegedProcessorTime);
                        Console.WriteLine("  total processor time: {0}",
                            myProcess.TotalProcessorTime);

                        // Update the values for the overall peak memory statistics.
                        peakPagedMem = myProcess.PeakPagedMemorySize64;
                        peakVirtualMem = myProcess.PeakVirtualMemorySize64;
                        peakWorkingSet = myProcess.PeakWorkingSet64;

                        if (myProcess.Responding)
                        {
                            Console.WriteLine("Status = Running");
                        }
                        else
                        {
                            Console.WriteLine("Status = Not Responding");
                        }
                    }
                }
                while (!myProcess.WaitForExit(1000));


                Console.WriteLine();
                Console.WriteLine("Process exit code: {0}", 
                    myProcess.ExitCode);
            
                // Display peak memory statistics for the process.
                Console.WriteLine("Peak physical memory usage of the process: {0}",
                    peakWorkingSet);
                Console.WriteLine("Peak paged memory usage of the process: {0}",
                    peakPagedMem);
                Console.WriteLine("Peak virtual memory usage of the process: {0}",
                    peakVirtualMem);

            } 
            finally
            {
                if (myProcess != null)
                {
                    myProcess.Close();
                }
            }
        }

    }
}
#using <system.dll>

using namespace System;
using namespace System::Diagnostics;
int main()
{
   
   // Define variables to track the peak
   // memory usage of the process.
   _int64 peakPagedMem = 0,peakWorkingSet = 0,peakVirtualMem = 0;
   Process^ myProcess = nullptr;
   try
   {
      
      // Start the process.
      myProcess = Process::Start( "NotePad.exe" );
      
      // Display the process statistics until
      // the user closes the program.
      do
      {
         if (  !myProcess->HasExited )
         {
            
            // Refresh the current process property values.
            myProcess->Refresh();
            Console::WriteLine();
            
            // Display current process statistics.
            Console::WriteLine( "{0} -", myProcess );
            Console::WriteLine( "-------------------------------------" );
            Console::WriteLine( "  physical memory usage: {0}", myProcess->WorkingSet64 );
            Console::WriteLine( "  base priority: {0}", myProcess->BasePriority );
            Console::WriteLine( "  priority class: {0}", myProcess->PriorityClass );
            Console::WriteLine( "  user processor time: {0}", myProcess->UserProcessorTime );
            Console::WriteLine( "  privileged processor time: {0}", myProcess->PrivilegedProcessorTime );
            Console::WriteLine( "  total processor time: {0}", myProcess->TotalProcessorTime );
            
            // Update the values for the overall peak memory statistics.
            peakPagedMem = myProcess->PeakPagedMemorySize64;
            peakVirtualMem = myProcess->PeakVirtualMemorySize64;
            peakWorkingSet = myProcess->PeakWorkingSet64;
            if ( myProcess->Responding )
            {
               Console::WriteLine( "Status = Running" );
            }
            else
            {
               Console::WriteLine( "Status = Not Responding" );
            }
         }
      }
      while (  !myProcess->WaitForExit( 1000 ) );
      Console::WriteLine();
      Console::WriteLine( "Process exit code: {0}", myProcess->ExitCode );
      
      // Display peak memory statistics for the process.
      Console::WriteLine( "Peak physical memory usage of the process: {0}", peakWorkingSet );
      Console::WriteLine( "Peak paged memory usage of the process: {0}", peakPagedMem );
      Console::WriteLine( "Peak virtual memory usage of the process: {0}", peakVirtualMem );
   }
   finally
   {
      if ( myProcess != nullptr )
      {
         myProcess->Close();
      }
   }

}

.NET Framework 安全性

平台

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Process 类
Process 成员
System.Diagnostics 命名空间
HasExited
CloseMainWindow
Kill
WaitForExit
Process.EnableRaisingEvents 属性