c#检测目标进程的运行时间,c#高手进!!
发布网友
发布时间:2022-05-02 05:04
我来回答
共1个回答
热心网友
时间:2022-06-28 11:44
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace CatchPic
{
class Program
{
static void Main(string[] args)
{
CatchPicInstance catchpic = new CatchPicInstance();
catchpic.Run("a.exe");
// 显示进程a.exe的运行时间。
// MessageBox.Show(CatchPicInstance.processTime);
}
}
class CatchPicInstance
{
private System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
public static processTime = 0;//运行时间
private string processNames = "";//如:QQ.exe
public void Run(string processNames)
{
this.processNames = processNames;
this.tmr.Interval = 1000;//每秒运行
this.tmr.Tick += new System.EventHandler(this.tmr_Tick);
this.tmr.Enabled = true;
}
private void tmr_Tick(object sender, EventArgs e)
{
if (FindProcessByName(processNames))
processTime += 1;
}
// 监测是否有a.exe进程。
static bool FindProcessByName(string processNames) //QQ.exe
{
string[] processName = processNames.Split(',');
System.Diagnostics.Process[] myProcess = System.Diagnostics.Process.GetProcesses();
for (int i = 0; i < myProcess.Length; i++)
{
string MoleName = "";
try
{
MoleName = myProcess[i].MainMole.MoleName;
}
catch
{
}
for (int j = 0; j < processName.Length; j++)
{
if (MoleName.ToLower() == processName[j].ToLower())
{
return true;
}
}
}
return false;
}
}
}
不懂可以问我.