Kev*_*vin 13 c# events process
创建新进程时是否有任何事件.我正在编写检查某些进程的ac#应用程序,但我不想写一个无限循环来连续迭代所有已知进程.相反,我宁愿检查每个创建的进程,也可以遍历事件触发的所有当前进程.有什么建议?
Process[] pArray;
while (true)
{
pArray = Process.GetProcesses();
foreach (Process p in pArray)
{
foreach (String pName in listOfProcesses) //just a list of process names to search for
{
if (pName.Equals(p.ProcessName, StringComparison.CurrentCultureIgnoreCase))
{
//do some stuff
}
}
}
Thread.Sleep(refreshRate * 1000);
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*tta 16
WMI为您提供了一种监听流程创建的方法(以及大约一百万个其他内容).在这里看到我的答案.
void WaitForProcess()
{
ManagementEventWatcher startWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
startWatch.EventArrived
+= new EventArrivedEventHandler(startWatch_EventArrived);
startWatch.Start();
}
static void startWatch_EventArrived(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Process started: {0}"
, e.NewEvent.Properties["ProcessName"].Value);
if (this is the process I'm interested in)
{
startWatch.Stop();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5256 次 |
| 最近记录: |