查找新的过程中使用C#开始 [英] Look Up for new process started using c#

查看:140
本文介绍了查找新的过程中使用C#开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来查找任何新的进程启动。此功能在线程中运行。



我需要登录进程名开始。对于我使用两个数组列表。在一个ArrayList的
I存储中的所有进程名启动线程和其他数组列表前,我填
内螺纹当前进程和比较两个数组列表,找到新的进程。



现在的问题是,因为字符串写入日志文件是在每个循环中,我看到
在日志文件中反复进程名。我希望它是一次记录。我怎样才能解决这个问题?



 类的ProcessMonitor 
{
公共静态的ArrayList ExistingProcess =新的ArrayList() ;


公共静态无效监视器()
{
existingProcesses = GetExistingProcess();

,而(真)
{
ArrayList的currentProcesses =新的ArrayList();
currentProcesses = GetCurrentProcess();

ArrayList的NewApps =新的ArrayList(GetCurrentProcess());

的foreach(在ExistingProcess变种ρ)
{
NewApps.Remove(对);
}
字符串str =;
的foreach(字符串NEWAPP在NewApps)
{
海峡+ =推出ProcessName / ID:+ NEWAPP +/+ System.Diagnostics.Process.GetProcessesByName(NEWAPP)[0] .Id.ToString()+ Environment.NewLine;
}
如果(STR =!)
{
Log.Info(STR);
}
}
}

公共静态的ArrayList GetExistingProcess()
{
过程[] = PROCESSLIST Process.GetProcesses();
的foreach(在PROCESSLIST过程PROC)
{
ExistingProcess.Add(Proc.ProcessName);
}
返回ExistingProcess;
}
公共静态的ArrayList GetCurrentProcess()
{
ArrayList的CurrentProcesses =新的ArrayList();
过程[] = PROCESSLIST Process.GetProcesses();
的foreach(在PROCESSLIST过程PROC)
{
CurrentProcesses.Add(Proc.ProcessName);
}
返回CurrentProcesses;
}
}


解决方案

迭代进程是Windows很贵。有一个更好的方式与WMI,Win32_ProcessStartTrace类来做到这一点。因为它只会告诉你新的流程入门它还可以自动解决您的问题。而并不需要一个线程。



您会发现你的这个答案


I am using the below code to look up for any new process started. This function is running in the thread.

I need to log in the process name started. For which I use two arraylist. On one arraylist i store all the process names before it starts the thread and the other arraylist, I fill the current process inside the thread and compare two arraylist to find the new process.

The problem now is, since the string to write to the log file is in for each loop, I see repeated process names in the log file. I want it to be logged only once. How can I solve this?

class ProcessMonitor
{
    public static ArrayList ExistingProcess = new ArrayList(); 


    public static void Monitor()
    { 
        existingProcesses = GetExistingProcess();

        while (true)
        {  
            ArrayList currentProcesses = new ArrayList();
            currentProcesses = GetCurrentProcess();

            ArrayList NewApps = new ArrayList(GetCurrentProcess());

            foreach (var p in ExistingProcess)
            {
                NewApps.Remove(p); 
            }
            string str = "";
            foreach (string NewApp in NewApps)
            {
                str += "Launched ProcessName/ID : " + NewApp + "/" + System.Diagnostics.Process.GetProcessesByName(NewApp)[0].Id.ToString() + Environment.NewLine;
            }
             if(str!="")
            {
               Log.Info(str);
            }
        }
    }

    public static ArrayList GetExistingProcess()
    {
        Process[] processlist = Process.GetProcesses();
        foreach (Process Proc in processlist)
        {
            ExistingProcess.Add(Proc.ProcessName);
        }
        return ExistingProcess;
    }
    public static ArrayList GetCurrentProcess()
    {
        ArrayList CurrentProcesses = new ArrayList();
        Process[] processlist =  Process.GetProcesses();
        foreach (Process Proc in processlist)
        {
            CurrentProcesses.Add(Proc.ProcessName);
        }
        return CurrentProcesses;
    }
}

解决方案

Iterating processes is very expensive on Windows. There's a better way to do it with WMI, Win32_ProcessStartTrace class. It also automatically solves your problem since it will only tell you about new processes getting started. And doesn't need a thread.

You'll find the code you need in this answer.

这篇关于查找新的过程中使用C#开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆