English 中文(简体)
概览 启动的新程序
原标题:Look Up for new process started using c#
  • 时间:2010-08-11 03:34:21
  •  标签:
  • c#

我正在使用以下法典来研究启动的任何新进程。 这一职能正在read。

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;
    }
}
最佳回答

视窗上的循环过程非常昂贵。 更妥善地与海事委员会合作,Win32-ProcessStart 追查组。 这也自动解决了你的问题,因为它只会告诉你新的进程已经开始。 不需要透镜。

阁下认为,你需要在上填写。

问题回答

我不确切地肯定你在此做些什么,但从下文摘录的头两条线和最后一行基本上都是一样的,只有最后一行的成本更高(因为你从通过<代码”返回的那一个阵列名单上排出第二个阵列)。 出口产品:

ArrayList currentProcesses = new ArrayList();
currentProcesses = GetCurrentProcess();

ArrayList NewApps = new ArrayList(GetCurrentProcess());

其次,你似乎从未使用<条码>现行程序变量,只要我能够告诉......它的全部废物。 第三,如果程序名称有重复,为什么会成为一个问题? 同样的进程可以不止一次启动,不止一个进程同时进行,一个进程可能运行,停止,然后再进行等等。 程序名称不一定是“不正确”的两倍。

(UPDATE:原因之一是,你只读到已有的Processes。 每次通过假体(以最大速度连续发生)时,你都会再次获得程序清单,并将程序清单与原有的<密码>现有程序<<>代码/代码”进行比较,因此,将重新列出以前文件中列出的相同程序。 我更新了我的法典榜样,以表明如何解决这一问题。

你似乎有一些根本的法典错误,而且你的期望可能有缺陷。 我将重新审视你的一般守则,消除无用守则(如上头两条),并普遍精简你的法典。 (Hint:ArrayList is a REALLY坏 choice择...... 我将使用<条码>电子计算法;T>,这并不要求任何来源的转换或来自原始轨道的人口。 如果我重复上述守则,则采用更有效的守则:

public static void Monitor()
{ 
    var existingProcesses = Process.GetProcesses();

    bool doProcessing = true;
    while (doProcessing)
    {  
        var currentProcesses = Process.GetProcesses();

        var newProcesses = currentProcesses.Except(existingProcesses);

        int capacity = newProcesses.Count() * 60;
        var builder = new StringBuilder(capacity);
        foreach (var newProcess in newProcesses)
        {
            builder.Append("Launched ProcessName/ID : ");
            builder.Append(newProcess.ProcessName);
            builder.Append("/");
            builder.Append(newProcess.Id);
            builder.AppendLine();
        }

        string newProcessLogEntry = builder.ToString();
        if(!String.IsNullOrEmpty(newProcessLogEntry))
        {
           Log.Info(newProcessLogEntry);
        }

        existingProcesses = currentProcesses;  // Update existing processes, so you don t reprocess previously processed running apps and get "duplicate log entries"

        if (requestToStopMonitoring) // do something to kill this loop gracefully at some point
        {
            doProcessing = false;
            continue;
        }

        Thread.Sleep(5000); // Wait about 5 seconds before iterating again
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签