Bagikan melalui


Process.Id Properti

Definisi

Mendapatkan pengidentifikasi unik untuk proses terkait.

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

Nilai Properti

Pengidentifikasi unik yang dihasilkan sistem dari proses yang dirujuk oleh instans ini Process .

Pengecualian

Properti proses Id belum ditetapkan.

-atau-

Tidak ada proses yang terkait dengan objek ini Process .

Contoh

Contoh berikut menunjukkan cara mendapatkan Id untuk semua instans aplikasi yang sedang berjalan. Kode membuat instans baru Notepad, mencantumkan semua instans Notepad, lalu memungkinkan pengguna untuk memasukkan Id nomor untuk menghapus instans tertentu.

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

Keterangan

Proses Id ini tidak valid jika proses terkait tidak berjalan. Oleh karena itu, Anda harus memastikan bahwa proses berjalan sebelum mencoba mengambil Id properti . Sampai proses berakhir, pengidentifikasi proses secara unik mengidentifikasi proses di seluruh sistem.

Anda dapat menyambungkan proses yang berjalan di komputer lokal atau jarak jauh ke instans baru Process dengan meneruskan pengidentifikasi proses ke GetProcessById metode . GetProcessById adalah static metode yang membuat komponen baru dan mengatur properti untuk instans Id baru Process secara otomatis.

Pengidentifikasi proses dapat digunakan kembali oleh sistem. Nilai Id properti hanya unik saat proses terkait sedang berjalan. Setelah proses dihentikan, sistem dapat menggunakan Id kembali nilai properti untuk proses yang tidak terkait.

Karena pengidentifikasi unik pada sistem, Anda dapat meneruskannya ke utas lain sebagai alternatif untuk meneruskan Process instans. Tindakan ini dapat menyimpan sumber daya sistem namun menjamin bahwa proses diidentifikasi dengan benar.

Berlaku untuk

Lihat juga