Bagikan melalui


Process.GetProcessById Metode

Definisi

Membuat komponen baru Process , dan mengaitkannya dengan sumber daya proses yang ada yang Anda tentukan.

Overload

GetProcessById(Int32)

Mengembalikan komponen baru Process , mengingat pengidentifikasi proses di komputer lokal.

GetProcessById(Int32, String)

Mengembalikan komponen baru Process , mengingat pengidentifikasi proses dan nama komputer di jaringan.

GetProcessById(Int32)

Sumber:
Process.cs
Sumber:
Process.cs
Sumber:
Process.cs

Mengembalikan komponen baru Process , mengingat pengidentifikasi proses di komputer lokal.

public:
 static System::Diagnostics::Process ^ GetProcessById(int processId);
public static System.Diagnostics.Process GetProcessById (int processId);
static member GetProcessById : int -> System.Diagnostics.Process
Public Shared Function GetProcessById (processId As Integer) As Process

Parameter

processId
Int32

Pengidentifikasi unik sistem dari sumber daya proses.

Mengembalikan

Komponen Process yang terkait dengan sumber daya proses lokal yang diidentifikasi oleh processId parameter .

Pengecualian

Proses yang ditentukan oleh processId parameter tidak berjalan. Pengidentifikasi mungkin kedaluwarsa.

Contoh

Contoh berikut mengambil informasi proses saat ini, proses yang berjalan di komputer lokal, semua instans Notepad yang berjalan di komputer lokal, dan proses tertentu di komputer lokal. Kemudian mengambil informasi untuk proses yang sama pada komputer jarak jauh.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{   
   // Get the current process.    
   Process^ currentProcess = Process::GetCurrentProcess();

   // Get all processes running on the local computer.
   array<Process^>^localAll = Process::GetProcesses();

   // Get all instances of Notepad running on the local computer.
   // This will return an empty array if notepad isn't running.
   array<Process^>^localByName = Process::GetProcessesByName("notepad");

   // Get a process on the local computer, using the process id.
   // This will throw an exception if there is no such process.
   Process^ localById = Process::GetProcessById(1234);


   // Get processes running on a remote computer. Note that this
   // and all the following calls will timeout and throw an exception
   // if "myComputer" and 169.0.0.0 do not exist on your local network.

   // Get all processes on a remote computer.
   array<Process^>^remoteAll = Process::GetProcesses("myComputer");

   // Get all instances of Notepad running on the specific computer, using machine name.
   array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );
   
   // Get all instances of Notepad running on the specific computer, using IP address.
   array<Process^>^ipByName = Process::GetProcessesByName( "notepad", "169.0.0.0" );
   
   // Get a process on a remote computer, using the process id and machine name.
   Process^ remoteById = Process::GetProcessById( 2345, "myComputer" );
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();

            // Get all processes running on the local computer.
            Process[] localAll = Process.GetProcesses();

            // Get all instances of Notepad running on the local computer.
            // This will return an empty array if notepad isn't running.
            Process[] localByName = Process.GetProcessesByName("notepad");

            // Get a process on the local computer, using the process id.
            // This will throw an exception if there is no such process.
            Process localById = Process.GetProcessById(1234);

            // Get processes running on a remote computer. Note that this
            // and all the following calls will timeout and throw an exception
            // if "myComputer" and 169.0.0.0 do not exist on your local network.

            // Get all processes on a remote computer.
            Process[] remoteAll = Process.GetProcesses("myComputer");

            // Get all instances of Notepad running on the specific computer, using machine name.
            Process[] remoteByName = Process.GetProcessesByName("notepad", "myComputer");

            // Get all instances of Notepad running on the specific computer, using IP address.
            Process[] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");

            // Get a process on a remote computer, using the process id and machine name.
            Process remoteById = Process.GetProcessById(2345, "myComputer");
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        Sub BindToRunningProcesses()
            ' Get the current process. You can use currentProcess from this point
            ' to access various properties and call methods to control the process.
            Dim currentProcess As Process = Process.GetCurrentProcess()

            ' Get all processes running on the local computer.
            Dim localAll As Process() = Process.GetProcesses()

            ' Get all instances of Notepad running on the local computer.
            ' This will return an empty array if notepad isn't running.
            Dim localByName As Process() = Process.GetProcessesByName("notepad")

            ' Get a process on the local computer, using the process id.
            ' This will throw an exception if there is no such process.
            Dim localById As Process = Process.GetProcessById(1234)


            ' Get processes running on a remote computer. Note that this
            ' and all the following calls will timeout and throw an exception
            ' if "myComputer" and 169.0.0.0 do not exist on your local network.

            ' Get all processes on a remote computer.
            Dim remoteAll As Process() = Process.GetProcesses("myComputer")

            ' Get all instances of Notepad running on the specific computer, using machine name.
            Dim remoteByName As Process() = Process.GetProcessesByName("notepad", "myComputer")

            ' Get all instances of Notepad running on the specific computer, using IP address.
            Dim ipByName As Process() = Process.GetProcessesByName("notepad", "169.0.0.0")

            ' Get a process on a remote computer, using the process id and machine name.
            Dim remoteById As Process = Process.GetProcessById(2345, "myComputer")
        End Sub

        Shared Sub Main()
            Dim myProcess As New MyProcess()
            myProcess.BindToRunningProcesses()
        End Sub

    End Class

End Namespace 'MyProcessSample

Keterangan

Gunakan metode ini untuk membuat komponen baru Process dan mengaitkannya dengan sumber daya proses di komputer lokal. Sumber daya proses harus sudah ada di komputer, karena GetProcessById(Int32) tidak membuat sumber daya sistem, melainkan mengaitkan sumber daya dengan komponen yang dihasilkan Process aplikasi. Proses Id hanya dapat diambil untuk proses yang saat ini berjalan di komputer. Setelah proses berakhir, GetProcessById(Int32) melemparkan pengecualian jika Anda meneruskannya pengidentifikasi yang kedaluwarsa.

Pada komputer tertentu, pengidentifikasi proses bersifat unik. GetProcessById(Int32) mengembalikan satu proses paling banyak. Jika Anda ingin mendapatkan semua proses yang menjalankan aplikasi tertentu, gunakan GetProcessesByName(String). Jika ada beberapa proses di komputer yang menjalankan aplikasi yang ditentukan, GetProcessesByName(String) mengembalikan array yang berisi semua proses terkait. Anda dapat mengkueri setiap proses ini pada gilirannya untuk pengidentifikasinya. Pengidentifikasi proses dapat dilihat di Processes panel Windows Task Manager. Kolom PID menampilkan pengidentifikasi proses yang ditetapkan ke proses.

Parameternya processId adalah Int32 (bilangan bulat bertanda tangan 32-bit), meskipun Windows API yang mendasar DWORD menggunakan (bilangan bulat 32-bit yang tidak ditandatangani) untuk API serupa. Ini karena alasan historis.

Lihat juga

Berlaku untuk

GetProcessById(Int32, String)

Sumber:
Process.cs
Sumber:
Process.cs
Sumber:
Process.cs

Mengembalikan komponen baru Process , mengingat pengidentifikasi proses dan nama komputer di jaringan.

public:
 static System::Diagnostics::Process ^ GetProcessById(int processId, System::String ^ machineName);
public static System.Diagnostics.Process GetProcessById (int processId, string machineName);
static member GetProcessById : int * string -> System.Diagnostics.Process
Public Shared Function GetProcessById (processId As Integer, machineName As String) As Process

Parameter

processId
Int32

Pengidentifikasi unik sistem dari sumber daya proses.

machineName
String

Nama komputer pada jaringan.

Mengembalikan

Komponen Process yang terkait dengan sumber daya proses jarak jauh yang diidentifikasi oleh processId parameter .

Pengecualian

Proses yang ditentukan oleh processId parameter tidak berjalan. Pengidentifikasi mungkin kedaluwarsa.

-atau-

Sintaks machineName parameter tidak valid. Nama mungkin memiliki panjang nol (0).

Parameternya machineName adalah null.

Contoh

Contoh berikut mengambil informasi proses saat ini, proses yang berjalan di komputer lokal, semua instans Notepad yang berjalan di komputer lokal, dan proses tertentu di komputer lokal. Kemudian mengambil informasi untuk proses yang sama pada komputer jarak jauh.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{   
   // Get the current process.    
   Process^ currentProcess = Process::GetCurrentProcess();

   // Get all processes running on the local computer.
   array<Process^>^localAll = Process::GetProcesses();

   // Get all instances of Notepad running on the local computer.
   // This will return an empty array if notepad isn't running.
   array<Process^>^localByName = Process::GetProcessesByName("notepad");

   // Get a process on the local computer, using the process id.
   // This will throw an exception if there is no such process.
   Process^ localById = Process::GetProcessById(1234);


   // Get processes running on a remote computer. Note that this
   // and all the following calls will timeout and throw an exception
   // if "myComputer" and 169.0.0.0 do not exist on your local network.

   // Get all processes on a remote computer.
   array<Process^>^remoteAll = Process::GetProcesses("myComputer");

   // Get all instances of Notepad running on the specific computer, using machine name.
   array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );
   
   // Get all instances of Notepad running on the specific computer, using IP address.
   array<Process^>^ipByName = Process::GetProcessesByName( "notepad", "169.0.0.0" );
   
   // Get a process on a remote computer, using the process id and machine name.
   Process^ remoteById = Process::GetProcessById( 2345, "myComputer" );
}
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();

            // Get all processes running on the local computer.
            Process[] localAll = Process.GetProcesses();

            // Get all instances of Notepad running on the local computer.
            // This will return an empty array if notepad isn't running.
            Process[] localByName = Process.GetProcessesByName("notepad");

            // Get a process on the local computer, using the process id.
            // This will throw an exception if there is no such process.
            Process localById = Process.GetProcessById(1234);

            // Get processes running on a remote computer. Note that this
            // and all the following calls will timeout and throw an exception
            // if "myComputer" and 169.0.0.0 do not exist on your local network.

            // Get all processes on a remote computer.
            Process[] remoteAll = Process.GetProcesses("myComputer");

            // Get all instances of Notepad running on the specific computer, using machine name.
            Process[] remoteByName = Process.GetProcessesByName("notepad", "myComputer");

            // Get all instances of Notepad running on the specific computer, using IP address.
            Process[] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");

            // Get a process on a remote computer, using the process id and machine name.
            Process remoteById = Process.GetProcessById(2345, "myComputer");
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        Sub BindToRunningProcesses()
            ' Get the current process. You can use currentProcess from this point
            ' to access various properties and call methods to control the process.
            Dim currentProcess As Process = Process.GetCurrentProcess()

            ' Get all processes running on the local computer.
            Dim localAll As Process() = Process.GetProcesses()

            ' Get all instances of Notepad running on the local computer.
            ' This will return an empty array if notepad isn't running.
            Dim localByName As Process() = Process.GetProcessesByName("notepad")

            ' Get a process on the local computer, using the process id.
            ' This will throw an exception if there is no such process.
            Dim localById As Process = Process.GetProcessById(1234)


            ' Get processes running on a remote computer. Note that this
            ' and all the following calls will timeout and throw an exception
            ' if "myComputer" and 169.0.0.0 do not exist on your local network.

            ' Get all processes on a remote computer.
            Dim remoteAll As Process() = Process.GetProcesses("myComputer")

            ' Get all instances of Notepad running on the specific computer, using machine name.
            Dim remoteByName As Process() = Process.GetProcessesByName("notepad", "myComputer")

            ' Get all instances of Notepad running on the specific computer, using IP address.
            Dim ipByName As Process() = Process.GetProcessesByName("notepad", "169.0.0.0")

            ' Get a process on a remote computer, using the process id and machine name.
            Dim remoteById As Process = Process.GetProcessById(2345, "myComputer")
        End Sub

        Shared Sub Main()
            Dim myProcess As New MyProcess()
            myProcess.BindToRunningProcesses()
        End Sub

    End Class

End Namespace 'MyProcessSample

Keterangan

Gunakan metode ini untuk membuat komponen baru Process dan mengaitkannya dengan sumber daya proses pada komputer jarak jauh pada jaringan. Sumber daya proses harus sudah ada di komputer yang ditentukan, karena GetProcessById(Int32, String) tidak membuat sumber daya sistem, melainkan mengaitkan sumber daya dengan komponen yang dihasilkan Process aplikasi. Proses Id hanya dapat diambil untuk proses yang saat ini berjalan di komputer. Setelah proses berakhir, GetProcessById(Int32, String) melemparkan pengecualian jika Anda meneruskannya pengidentifikasi yang kedaluwarsa.

Pada komputer tertentu, pengidentifikasi proses bersifat unik. GetProcessById(Int32, String) mengembalikan satu proses paling banyak. Jika Anda ingin mendapatkan semua proses yang menjalankan aplikasi tertentu, gunakan GetProcessesByName(String). Jika ada beberapa proses di komputer yang menjalankan aplikasi yang ditentukan, GetProcessesByName(String) mengembalikan array yang berisi semua proses terkait. Anda dapat mengkueri setiap proses ini pada gilirannya untuk pengidentifikasinya. Pengidentifikasi proses dapat dilihat di Processes panel Windows Task Manager. Kolom PID menampilkan pengidentifikasi proses yang ditetapkan ke proses.

Jika Anda tidak menentukan machineName, komputer lokal akan digunakan. Atau, Anda dapat menentukan komputer lokal dengan mengatur machineName ke nilai "." atau ke string kosong ("").

Parameternya processId adalah Int32 (bilangan bulat bertanda tangan 32-bit), meskipun Windows API yang mendasar DWORD menggunakan (bilangan bulat 32-bit yang tidak ditandatangani) untuk API serupa. Ini karena alasan historis.

Lihat juga

Berlaku untuk