Process.ExitCode Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lekéri azt az értéket, amelyet a társított folyamat a leállításakor adott meg.
public:
property int ExitCode { int get(); };
public int ExitCode { get; }
[System.ComponentModel.Browsable(false)]
public int ExitCode { get; }
member this.ExitCode : int
[<System.ComponentModel.Browsable(false)>]
member this.ExitCode : int
Public ReadOnly Property ExitCode As Integer
Tulajdonság értéke
Az a kód, amelyet a társított folyamat a leállításakor adott meg.
- Attribútumok
Kivételek
Egy távoli számítógépen futó folyamat tulajdonságához próbál hozzáférni ExitCode . Ez a tulajdonság csak a helyi számítógépen futó folyamatokhoz érhető el.
Példák
Az alábbi példa elindítja a Jegyzettömb egy példányát. A példa ezután lekéri és megjeleníti a társított folyamat különböző tulajdonságait. A példa észleli, hogy a folyamat mikor lép ki, és megjeleníti a folyamat kilépési kódját.
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;
// Start the process.
using (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($"{myProcess} -");
Console.WriteLine("-------------------------------------");
Console.WriteLine($" Physical memory usage : {myProcess.WorkingSet64}");
Console.WriteLine($" Base priority : {myProcess.BasePriority}");
Console.WriteLine($" Priority class : {myProcess.PriorityClass}");
Console.WriteLine($" User processor time : {myProcess.UserProcessorTime}");
Console.WriteLine($" Privileged processor time : {myProcess.PrivilegedProcessorTime}");
Console.WriteLine($" Total processor time : {myProcess.TotalProcessorTime}");
Console.WriteLine($" Paged system memory size : {myProcess.PagedSystemMemorySize64}");
Console.WriteLine($" Paged memory size : {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 : {myProcess.ExitCode}");
// Display peak memory statistics for the process.
Console.WriteLine($" Peak physical memory usage : {peakWorkingSet}");
Console.WriteLine($" Peak paged memory usage : {peakPagedMem}");
Console.WriteLine($" Peak virtual memory usage : {peakVirtualMem}");
}
}
}
}
open System.Diagnostics
// Define variables to track the peak
// memory usage of the process.
let mutable peakPagedMem = 0L
let mutable peakWorkingSet = 0L
let mutable peakVirtualMem = 0L
// Start the process.
use myProcess = Process.Start "NotePad.exe"
// Display the process statistics until
// the user closes the program.
while myProcess.WaitForExit 1000 |> not do
if not myProcess.HasExited then
// Refresh the current process property values.
myProcess.Refresh()
printfn ""
// Display current process statistics.
printfn $"{myProcess} -"
printfn "-------------------------------------"
printfn $" Physical memory usage : {myProcess.WorkingSet64}"
printfn $" Base priority : {myProcess.BasePriority}"
printfn $" Priority class : {myProcess.PriorityClass}"
printfn $" User processor time : {myProcess.UserProcessorTime}"
printfn $" Privileged processor time : {myProcess.PrivilegedProcessorTime}"
printfn $" Total processor time : {myProcess.TotalProcessorTime}"
printfn $" Paged system memory size : {myProcess.PagedSystemMemorySize64}"
printfn $" Paged memory size : {myProcess.PagedMemorySize64}"
// Update the values for the overall peak memory statistics.
peakPagedMem <- myProcess.PeakPagedMemorySize64
peakVirtualMem <- myProcess.PeakVirtualMemorySize64
peakWorkingSet <- myProcess.PeakWorkingSet64
if myProcess.Responding then
printfn "Status = Running"
else
printfn "Status = Not Responding"
printfn ""
printfn $" Process exit code : {myProcess.ExitCode}"
// Display peak memory statistics for the process.
printfn $" Peak physical memory usage : {peakWorkingSet}"
printfn $" Peak paged memory usage : {peakPagedMem}"
printfn $" Peak virtual memory usage : {peakVirtualMem}"
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
' Start the process.
Using 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($"{myProcess} -")
Console.WriteLine("-------------------------------------")
Console.WriteLine($" Physical memory usage : {myProcess.WorkingSet64}")
Console.WriteLine($" Base priority : {myProcess.BasePriority}")
Console.WriteLine($" Priority class : {myProcess.PriorityClass}")
Console.WriteLine($" User processor time : {myProcess.UserProcessorTime}")
Console.WriteLine($" Privileged processor time : {myProcess.PrivilegedProcessorTime}")
Console.WriteLine($" Total processor time : {myProcess.TotalProcessorTime}")
Console.WriteLine($" Paged system memory size : {myProcess.PagedSystemMemorySize64}")
Console.WriteLine($" Paged memory size : {myProcess.PagedMemorySize64}")
' 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 : {myProcess.ExitCode}")
' Display peak memory statistics for the process.
Console.WriteLine($" Peak physical memory usage of the process : {peakWorkingSet}")
Console.WriteLine($" Peak paged memory usage of the process : {peakPagedMem}")
Console.WriteLine($" Peak virtual memory usage of the process : {peakVirtualMem}")
End Using
End Sub
End Class
End Namespace
Megjegyzések
A rendszerfolyamat kilépéskor visszaadott állapotának lekérésére használható ExitCode . A kilépési kódot ugyanúgy használhatja, mint egy eljárás egész számának visszatérési main() értékét.
A ExitCode folyamat értéke az alkalmazás fejlesztője által a folyamathoz implementált konkrét konvenciónak felel meg. Ha a kilépési kód értéket használod döntések meghozatalához a kódodban, győződj meg róla, hogy ismered az alkalmazási folyamat által használt kilépési kód konvenciót.
A fejlesztők általában nulla értékkel jelzik a sikeres kilépést ExitCode , és nemero értékekkel jelölik meg a hibákat, amelyeket a hívási módszer a rendellenes folyamatkimaradás okának azonosítására használhat. Ezeket az iránymutatásokat nem kell követni, de ezek az egyezmények.
Ha megpróbálja lekérni a ExitCode folyamatból való kilépés előtti értéket, a kísérlet kivételt eredményez. Először vizsgálja meg a HasExited tulajdonságot annak ellenőrzéséhez, hogy a társított folyamat leállt-e.
Note
Ha a standard kimenetet átirányították az aszinkron eseménykezelőkre, lehetséges, hogy a kimenet feldolgozása nem fejeződik be a visszatéréskor HasExitedtrue. Az aszinkron eseménykezelés befejezésének biztosításához hívja meg a paramétert nem használó túlterhelést az WaitForExit() ellenőrzés HasExitedelőtt.
A kapcsolódó folyamat kilépését a metódussal vagy a CloseMainWindowKill metódussal is megteheti.
A kapcsolódó folyamat kilépésekor kétféleképpen lehet értesítést kapni: szinkron és aszinkron módon. A szinkron értesítés a metódus meghívásával WaitForExit szünetelteti az alkalmazás feldolgozását, amíg a társított összetevő ki nem lép. Az aszinkron értesítés az Exited eseményre támaszkodik. Aszinkron értesítés EnableRaisingEvents használatakor az összetevőnek be kell állítania true , Process hogy értesítést kapjon arról, hogy a folyamat kilépett.