如何使用c#编写一个小程序,完成以下功能:当程序启动时,自动检测所有进程,以发现目标进程a,若a存在,则启动程序的计时模块。若a进程未运行,则对进程经行实时监控,一旦发现目标进程a启动,则自动运行程序的计时模块。若进程a在某时间终止,则同时停止程序的计时。
各位c#的高手,教教我呗!谢谢!!!
最好是哪位高手给出具体关键代码和算法,小弟拜谢!!!
c#检测目标进程的运行时间,c#高手进!!
答案:1 悬赏:80 手机版
解决时间 2021-03-06 02:11
- 提问者网友:临风不自傲
- 2021-03-05 18:41
最佳答案
- 五星知识达人网友:神也偏爱
- 2021-03-05 19:29
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的运行时间。62616964757a686964616fe78988e69d8331333262356664
// 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 ModuleName = "";
try
{
ModuleName = myProcess[i].MainModule.ModuleName;
}
catch
{
}
for (int j = 0; j < processName.Length; j++)
{
if (ModuleName.ToLower() == processName[j].ToLower())
{
return true;
}
}
}
return false;
}
}
}
不懂可以问我.
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的运行时间。62616964757a686964616fe78988e69d8331333262356664
// 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 ModuleName = "";
try
{
ModuleName = myProcess[i].MainModule.ModuleName;
}
catch
{
}
for (int j = 0; j < processName.Length; j++)
{
if (ModuleName.ToLower() == processName[j].ToLower())
{
return true;
}
}
}
return false;
}
}
}
不懂可以问我.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯