Process.Id 속성

정의

연결된 프로세스의 고유 식별자를 가져옵니다.

public:
 property int Id { int get(); };
public int Id { get; }
member this.Id : int
Public ReadOnly Property Id As Integer

속성 값

Process 인스턴스에서 참조하는 프로세스의 고유 식별자입니다. 이 식별자는 시스템에서 생성됩니다.

예외

프로세스의 Id 속성이 설정되어 있지 않습니다.

또는

Process 개체와 연결된 프로세스가 없습니다.

예제

다음 예제에서는 가져오는 방법을 보여 줍니다는 Id 모든 애플리케이션의 인스턴스를 실행 합니다. 이 코드는 메모장의 새 instance 만들고 메모장의 모든 인스턴스를 나열한 다음 사용자가 숫자를 입력 Id 하여 특정 instance 제거할 수 있도록 합니다.

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;
        }
    }
}
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 하기 전에 프로세스가 실행 중인지 확인해야 합니다. 프로세스가 종료될 때까지 프로세스 식별자는 시스템 전체에서 프로세스를 고유하게 식별합니다.

메서드에 프로세스 식별자를 GetProcessById 전달하여 로컬 또는 원격 컴퓨터에서 실행되는 프로세스를 새 Process instance 연결할 수 있습니다. GetProcessByIdstatic 새 구성 요소를 만들고 새 instance 대한 Process 속성을 자동으로 설정하는 Id 메서드입니다.

시스템에서 프로세스 식별자를 다시 사용할 수 있습니다. 속성 값은 Id 연결된 프로세스가 실행되는 동안에만 고유합니다. 프로세스가 종료된 후 시스템은 관련 없는 프로세스에 Id 속성 값을 다시 사용할 수 있습니다.

식별자는 시스템에서 고유하기 때문에 instance 전달하는 대신 다른 스레드에 Process 전달할 수 있습니다. 이 작업은 시스템 리소스를 저장하면서도 프로세스가 올바르게 식별되도록 보장할 수 있습니다.

적용 대상

추가 정보