当前位置: 首页>>代码示例>>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;未经允许,请勿转载。