欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#获取进程或线程相关信息的方法

程序员文章站 2023-11-27 23:41:16
本文实例讲述了c#获取进程或线程相关信息的方法。分享给大家供大家参考。具体实现方法如下: using system; using system.collecti...

本文实例讲述了c#获取进程或线程相关信息的方法。分享给大家供大家参考。具体实现方法如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.diagnostics;
namespace consoleapp
{
 class processdo
 {
  /// <summary>
  /// 获取进程相关信息
  /// </summary>
  public static void getprocessinfomation()
  {
   process pro = process.getprocessesbyname("qq")[0];
   console.writeline("进程名称:" + pro.processname);
   console.writeline("进程id:" + pro.id.tostring());
   console.writeline("启动时间:" + pro.starttime.tolongdatestring() + pro.starttime.tolongtimestring());
   console.writeline("是否响应:" + pro.responding.tostring());
   console.writeline("关联进程句柄:" + pro.handle.tostring());
   console.writeline("进程打开的句柄数:" + pro.handlecount.tostring());
   console.writeline("主窗口句柄:" + pro.mainwindowhandle.tostring());
   console.writeline("主窗口标题:" + pro.mainwindowtitle);
   console.writeline("模块数量:" + pro.modules.count.tostring());
   console.writeline("基本优先级:" + pro.basepriority.tostring());
   console.writeline("提升优先级:" + pro.priorityboostenabled.tostring());
   console.writeline("处理器:" + pro.processoraffinity.toint32().tostring());
   console.writeline("最小工作集:" + pro.minworkingset.toint32().tostring());
   console.writeline("最大工作集:" + pro.maxworkingset.toint32().tostring());
   console.writeline("工作集:" + pro.workingset.tostring());
   console.writeline("峰值工作集:" + pro.peakworkingset.tostring());
   console.writeline("专用内存大小:" + pro.privatememorysize.tostring());
   console.writeline("未分页内存大小:" + pro.nonpagedsystemmemorysize.tostring());
   console.writeline("分页内存大小:" + pro.pagedmemorysize.tostring());
   console.writeline("峰值分页内存大小:" + pro.peakpagedmemorysize.tostring());
   console.writeline("虚拟内存大小:" + pro.virtualmemorysize.tostring());
   console.writeline("峰值虚拟内存大小:" + pro.peakvirtualmemorysize.tostring());
   console.writeline("占用时间:" + pro.totalprocessortime.tostring());
   console.writeline("特权占用时间:" + pro.privilegedprocessortime.tostring());
   console.writeline("用户占用时间:" + pro.userprocessortime.tostring());
  }
  /// <summary>
  /// 进程的线程详细信息
  /// </summary>
  public static void getprocessthreadinfomation()
  {
   process pro = process.getprocessesbyname("qq")[0];
   console.writeline("qq程序进程的线程详细信息如下:");
   int length = pro.threads.count;
   for (int i = 0; i < length; i++)
   {
    var thread = pro.threads[i];
    console.writeline("标识符:" + thread.id.tostring());
    console.writeline("基本优先级:" + thread.basepriority.tostring());
    console.writeline("当前优先级:" + thread.currentpriority.tostring());
    console.writeline("内存地址:" + thread.startaddress.toint32());
    console.writeline("启动时间:" + thread.starttime.tostring());
    console.writeline("使用时间:" + thread.userprocessortime.tostring());
    console.write("当前状态:");
    switch (thread.threadstate)
    {
     case threadstate.initialized:
      console.writeline("线程已经初始化但尚未启动");
      break;
     case threadstate.ready:
      console.writeline("线程准备在下一个可用的处理器上运行");
      break;
     case threadstate.running:
      console.writeline("当前正在使用处理器");
      break;
     case threadstate.standby:
      console.writeline("线程将要使用处理器");
      break;
     case threadstate.terminated:
      console.writeline("线程已完成执行并退出"); 
      break;
     case threadstate.transition:
      console.writeline("线程在可以执行钱等待处理器之外的资源");
      break;
     case threadstate.unknown:
      console.writeline("状态未知");
      break;
     case threadstate.wait:
      console.writeline("正在等待外围操作完成或者资源释放");
      break;
     default:
      break;
    }
    if (thread.threadstate == threadstate.wait)
    {
     console.write("等待原因:");
     switch (thread.waitreason)
     {
      case threadwaitreason.eventpairhigh:
       console.writeline("线程正在等待事件对高");
       break;
      case threadwaitreason.eventpairlow:
       console.writeline("线程正在等待事件对低");
       break;
      case threadwaitreason.executiondelay:
       console.writeline("线程执行延迟");
       break;
      case threadwaitreason.executive:
       console.writeline("线程正在等待计划程序");
       break;
      case threadwaitreason.freepage:
       console.writeline("线程正在等待可用的虚拟内存页");
       break;
      case threadwaitreason.lpcreceive:
       console.writeline("线程正在等待本地过程调用到达");
       break;
      case threadwaitreason.lpcreply:
       console.writeline("线程正在等待对本地过程调用的回复到达");
       break;
      case threadwaitreason.pagein:
       console.writeline("线程正在等待虚拟内存页到达内存");
       break;
      case threadwaitreason.pageout:
       console.writeline("线程正在等待虚拟内存页写入磁盘");
       break;
      case threadwaitreason.suspended:
       console.writeline("线程执行暂停");
       break;
      case threadwaitreason.systemallocation:
       console.writeline("线程正在等待系统分配");
       break;
      case threadwaitreason.unknown:
       console.writeline("线程因位置原因而等待");
       break;
      case threadwaitreason.userrequest:
       console.writeline("线程正在等待用户请求");
       break;
      case threadwaitreason.virtualmemory:
       console.writeline("线程正在等待系统分配虚拟内存");
       break;
      default:
       break;
     }
    }
    console.writeline();
    }
  }
  /// <summary>
  /// 限制应用程序运行时间
  /// 即关联程序超出设置的运行将自动关闭
  /// </summary>
  public static void getkillappforwaittime()
  {
   process proc = new process();
   proc.startinfo = new processstartinfo(@"c:\documents and settings\zkk\桌面\mysql_20120925.sql");
   //启动应用程序
   proc.start();
   //等待程序师徒完成载入
   proc.waitforinputidle();
   //等待程序结束执行
   proc.waitforexit(3000);
   //如果程序在时间期限之前关闭,hasexited将会是true
   if (proc.hasexited == false)
   {
    //测试程序是否已经停止回应
    if (proc.responding)
    {
     //程序有回应,关闭主视图
     proc.closemainwindow();
    }
    else
    {
     //如果程序没有回应,将强制关闭
     proc.kill();
    }
   }
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。