當前位置: 首頁>>代碼示例>>C#>>正文


C# Process.GetProcessById方法代碼示例

本文整理匯總了C#中System.Diagnostics.Process.GetProcessById方法的典型用法代碼示例。如果您正苦於以下問題:C# Process.GetProcessById方法的具體用法?C# Process.GetProcessById怎麽用?C# Process.GetProcessById使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Diagnostics.Process的用法示例。


在下文中一共展示了Process.GetProcessById方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: BindToRunningProcesses

//引入命名空間
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();
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Diagnostics,代碼行數:49,代碼來源:Process.GetProcessById

示例2: EnumThreadsForPid

//引入命名空間
using System;
using System.Diagnostics;

class MainClass
{
  public static void EnumThreadsForPid(int pID)
  {
    Process theProc;

    try {
      theProc = Process.GetProcessById(pID);
    } catch {
      Console.WriteLine("-> Sorry...bad PID!");
      return;
    }
    
    Console.WriteLine("Here are the thread IDs for: {0}", theProc.ProcessName);

    ProcessThreadCollection theThreads = theProc.Threads;
    foreach(ProcessThread pt in theThreads)
    {
      string info = string.Format("-> Thread ID: {0}\tStart Time {1}\tPriority {2}", pt.Id , pt.StartTime.ToShortTimeString(), pt.PriorityLevel);
      Console.WriteLine(info);
    }
  }

  static void Main(string[] args)
  {
    int theProcID = 10001;
    EnumThreadsForPid(theProcID);
  }
}
開發者ID:C#程序員,項目名稱:System.Diagnostics,代碼行數:33,代碼來源:Process.GetProcessById


注:本文中的System.Diagnostics.Process.GetProcessById方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。