亲宝软件园·资讯

展开

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

我心依旧 人气:0
想了解C#获取进程或线程相关信息的方法的相关内容吗,我心依旧在本文为您仔细讲解C#获取进程或线程相关信息的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,获取,进程,线程,相关信息,下面大家一起来学习吧。

本文实例讲述了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#程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论