Process.Id Eigenschaft
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ruft den eindeutigen Bezeichner für den zugeordneten Prozess ab.
public:
property int Id { int get(); };
public int Id { get; }
member this.Id : int
Public ReadOnly Property Id As Integer
Eigenschaftswert
Der vom System generierte eindeutige Bezeichner des Prozesses, auf den von dieser Process Instanz verwiesen wird.
Ausnahmen
Die Eigenschaft des Id Prozesses wurde nicht festgelegt.
-oder-
Diesem Process Objekt ist kein Prozess zugeordnet.
Beispiele
Im folgenden Beispiel wird veranschaulicht, wie Sie die Id für alle ausgeführten Instanzen einer Anwendung abrufen. Der Code erstellt eine neue Instanz von Editor, listet alle Instanzen von Editor auf und ermöglicht dem Benutzer dann die Eingabe der Id Nummer, um eine bestimmte Instanz zu entfernen.
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;
using System.Diagnostics;
class ProcessDemo
{
public static void Main()
{
Process notePad = Process.Start("notepad");
Console.WriteLine("Started notepad process Id = " + notePad.Id);
Console.WriteLine("All instances of notepad:");
// Get Process objects for all running instances on notepad.
Process[] localByName = Process.GetProcessesByName("notepad");
int i = localByName.Length;
while (i > 0)
{
// You can use the process Id to pass to other applications or to
// reference that particular instance of the application.
Console.WriteLine(localByName[i - 1].Id.ToString());
i -= 1;
}
i = localByName.Length;
while (i > 0)
{
Console.WriteLine("Enter a process Id to kill the process");
string id = Console.ReadLine();
if (string.IsNullOrEmpty(id))
break;
try
{
using (Process chosen = Process.GetProcessById(Int32.Parse(id)))
{
if (chosen.ProcessName == "notepad")
{
chosen.Kill();
chosen.WaitForExit();
}
}
}
catch (Exception e)
{
Console.WriteLine("Incorrect entry.");
continue;
}
i -= 1;
}
}
}
open System
open System.Diagnostics
let notePad = Process.Start "notepad"
printfn $"Started notepad process Id = {notePad.Id}"
printfn "All instances of notepad:"
// Get Process objects for all running instances on notepad.
let localByName = Process.GetProcessesByName "notepad"
let mutable i = localByName.Length
while i > 0 do
// You can use the process Id to pass to other applications or to
// reference that particular instance of the application.
printfn $"{localByName.[i - 1].Id}"
i <- i - 1
i <- localByName.Length
while i > 0 do
printfn "Enter a process Id to kill the process"
let id = Console.ReadLine()
if String.IsNullOrEmpty id then
i <- 0
else
try
use chosen = Int32.Parse id |> Process.GetProcessById
if chosen.ProcessName = "notepad" then
chosen.Kill()
chosen.WaitForExit()
i <- i - 1
with e ->
printfn "Incorrect entry."
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.Principal
Imports System.Diagnostics
Class ProcessDemo
Public Shared Sub Main()
Dim notePad As Process = Process.Start("notepad")
Console.WriteLine("Started notepad process Id = " + notePad.Id.ToString())
Console.WriteLine("All instances of notepad:")
' Get Process objects for all running instances on notepad.
Dim localByName As Process() = Process.GetProcessesByName("notepad")
Dim i As Integer = localByName.Length
While i > 0
' You can use the process Id to pass to other applications or to
' reference that particular instance of the application.
Console.WriteLine(localByName((i - 1)).Id.ToString())
i -= 1
End While
i = localByName.Length
While i > 0
Console.WriteLine("Enter a process Id to kill the process")
Dim id As String = Console.ReadLine()
If id = String.Empty Then
Exit While
End If
Try
Using chosen As Process = Process.GetProcessById(Int32.Parse(id))
If chosen.ProcessName = "notepad" Then
chosen.Kill()
chosen.WaitForExit()
End If
End Using
Catch e As Exception
Console.WriteLine("Incorrect entry.")
GoTo ContinueWhile1
End Try
i -= 1
ContinueWhile1:
End While
End Sub
End Class
Hinweise
Der Prozess Id ist ungültig, wenn der zugeordnete Prozess nicht ausgeführt wird. Daher sollten Sie sicherstellen, dass der Prozess ausgeführt wird, bevor Sie versuchen, die Id Eigenschaft abzurufen. Bis der Prozess beendet wird, identifiziert der Prozessbezeichner den Prozess im gesamten System eindeutig.
Sie können einen Prozess, der auf einem lokalen oder Remotecomputer ausgeführt wird, mit einer neuen Process Instanz verbinden, indem Sie die Prozess-ID an die GetProcessById Methode übergeben.
GetProcessById ist eine static Methode, die eine neue Komponente erstellt und die Id Eigenschaft für die neue Process Instanz automatisch festlegt.
Prozessbezeichner können vom System wiederverwendet werden. Der Id Eigenschaftswert ist nur eindeutig, wenn der zugeordnete Prozess ausgeführt wird. Nachdem der Prozess beendet wurde, kann das System den Id Eigenschaftswert für einen nicht verknüpften Prozess wiederverwenden.
Da der Bezeichner im System eindeutig ist, können Sie ihn als Alternative zum Übergeben einer Process Instanz an andere Threads übergeben. Diese Aktion kann Systemressourcen speichern und dennoch garantieren, dass der Prozess ordnungsgemäß identifiziert wird.