Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Propiedad Process.ExitCode

 

Publicado: octubre de 2016

Obtiene el valor especificado por el proceso asociado al terminar.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

[BrowsableAttribute(false)]
public int ExitCode { get; }

Valor de propiedad

Type: System.Int32

Código especificado por el proceso asociado al terminar.

Exception Condition
InvalidOperationException

El proceso no finalizó.

O bien

El proceso Handle no es válido.

NotSupportedException

Está intentando obtener acceso a la propiedad ExitCode de un proceso que se ejecuta en un equipo remoto. Esta propiedad está disponible únicamente para los procesos que se ejecutan en el equipo local.

Use P:System.Diagnostics.Process.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.

The P:System.Diagnostics.Process.ExitCode value for a process reflects the specific convention implemented by the application developer for that process. If you use the exit code value to make decisions in your code, be sure that you know the exit code convention used by the application process.

Developers usually indicate a successful exit by an P:System.Diagnostics.Process.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 P:System.Diagnostics.Process.ExitCode before the process has exited, the attempt throws an exception. Examine the P:System.Diagnostics.Process.HasExited property first to verify whether the associated process has terminated.

System_CAPS_noteNota

When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when P:System.Diagnostics.Process.HasExited returns true. To ensure that asynchronous event handling has been completed, call the M:System.Diagnostics.Process.WaitForExit overload that takes no parameter before checking P:System.Diagnostics.Process.HasExited.

You can use the M:System.Diagnostics.Process.CloseMainWindow or the M:System.Diagnostics.Process.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 M:System.Diagnostics.Process.WaitForExit(System.Int32) method to pause the processing of your application until the associated component exits. Asynchronous notification relies on the E:System.Diagnostics.Process.Exited event. When using asynchronous notification, P:System.Diagnostics.Process.EnableRaisingEvents must be set to true for the T:System.Diagnostics.Process component to receive notification that the process has exited.

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.

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);
                        Console.WriteLine("  PagedSystemMemorySize64: {0}",
                            myProcess.PagedSystemMemorySize64);
                        Console.WriteLine("  PagedMemorySize64: {0}",
                           myProcess.PagedMemorySize64);

                        // 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();
                }
            }
        }

    }
}

LinkDemand

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

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar: