Process.Id 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
연결된 프로세스의 고유 식별자를 가져옵니다.
public:
property int Id { int get(); };
public int Id { get; }
member this.Id : int
Public ReadOnly Property Id As Integer
속성 값
이 Process 인스턴스에서 참조하는 프로세스의 시스템 생성 고유 식별자입니다.
예외
예제
다음 예제에서는 애플리케이션의 실행 중인 모든 인스턴스에 Id 대해 가져오는 방법을 보여 줍니다. 이 코드는 메모장의 새 인스턴스를 만들고 메모장의 모든 인스턴스를 나열한 다음 사용자가 번호를 입력 Id 하여 특정 인스턴스를 제거할 수 있도록 합니다.
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
설명
연결된 프로세스가 Id 실행되고 있지 않으면 프로세스가 유효하지 않습니다. 따라서 속성을 검색 Id 하기 전에 프로세스가 실행 중인지 확인해야 합니다. 프로세스가 종료될 때까지 프로세스 식별자는 시스템 전체에서 프로세스를 고유하게 식별합니다.
메서드에 프로세스 식별자를 전달하여 로컬 또는 원격 컴퓨터에서 실행 중인 프로세스를 새 Process 인스턴스에 GetProcessById 연결할 수 있습니다.
GetProcessById 는 static 새 구성 요소를 만들고 새 인스턴스의 Id 속성을 Process 자동으로 설정하는 메서드입니다.
프로세스 식별자는 시스템에서 다시 사용할 수 있습니다. 속성 값은 Id 연결된 프로세스가 실행되는 동안에만 고유합니다. 프로세스가 종료되면 시스템에서 관련 없는 프로세스에 Id 속성 값을 다시 사용할 수 있습니다.
식별자는 시스템에서 고유하므로 인스턴스를 전달하는 Process 대신 다른 스레드에 전달할 수 있습니다. 이 작업은 시스템 리소스를 저장할 수 있지만 프로세스가 올바르게 식별되도록 보장할 수 있습니다.