Process.Id Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient l’identificateur unique du processus associé.
public:
property int Id { int get(); };
public int Id { get; }
member this.Id : int
Public ReadOnly Property Id As Integer
Valeur de propriété
Identificateur unique généré par le système du processus référencé par cette Process instance.
Exceptions
La propriété du Id processus n’a pas été définie.
-ou-
Aucun processus n’est associé à cet Process objet.
Exemples
L’exemple suivant montre comment obtenir les Id instances en cours d’exécution d’une application. Le code crée une instance du Bloc-notes, répertorie toutes les instances du Bloc-notes, puis permet à l’utilisateur d’entrer le Id numéro pour supprimer une instance spécifique.
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
Remarques
Le processus Id n’est pas valide si le processus associé n’est pas en cours d’exécution. Par conséquent, vous devez vous assurer que le processus est en cours d’exécution avant de tenter de récupérer la Id propriété. Jusqu’à ce que le processus se termine, l’identificateur de processus identifie de façon unique le processus dans tout le système.
Vous pouvez connecter un processus qui s’exécute sur un ordinateur local ou distant à une nouvelle Process instance en transmettant l’identificateur de processus à la GetProcessById méthode.
GetProcessById est une static méthode qui crée un composant et définit automatiquement la Id propriété pour la nouvelle Process instance.
Les identificateurs de processus peuvent être réutilisés par le système. La Id valeur de propriété est unique uniquement pendant l’exécution du processus associé. Une fois le processus terminé, le système peut réutiliser la Id valeur de propriété d’un processus non lié.
Étant donné que l’identificateur est unique sur le système, vous pouvez le transmettre à d’autres threads comme alternative à la transmission d’une Process instance. Cette action peut enregistrer les ressources système tout en garantissant que le processus est correctement identifié.