不断监视进程 [英] Constantly monitor processes

查看:137
本文介绍了不断监视进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些code可监控运行Windows程序。
我想知道当某一进程启动,当它结束。
在code作品就像我想让它。

I've written some code which monitors the running windows processes. I wish to know when a certain process starts and when it ends. The code works just like I want it to.

现在,我要实现它在Windows窗体中的服务器应用程序 - 因此它会循环,只要形式是活的运行。我想我应该让它运行异步也许使用的BackgroundWorker 。我只是不知道什么是应该做,以及如何最好的办法。

Now, I wish to implement it in a windows form server application - so it will run in loop as long as the form is alive. I guess I should make it run asynchronously maybe using a BackgroundWorker. I'm just not sure what's the best way to do it and how.

下面是我的显示器code:

Here is my monitor code:

using System;
using System.Management;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;

class ProcessMonitor
{
    public static void Main()
    {
        Dictionary<string, Process> newProcs= new Dictionary<string, Process> ();
        while (true) 
        {
            foreach (Process process in Process.GetProcesses())
            {
                if (process.ProcessName.CompareTo("Someprocess") == 0)
                {
                    if (!searchForProcess(newProcs, process.Id))
                    {
                        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id);
                        foreach (ManagementObject @object in searcher.Get())
                        {
                            Console.Write("Adding new Process: ");
                            Console.WriteLine(@object["CommandLine"] + " ");
                            newProcs.Add(@object["CommandLine"] + " ", process);
                        }   
                    }
                }
            }
            checkProcesses(newProcs);
        } 
        Console.WriteLine("Done");
    }

    private static bool searchForProcess(Dictionary<string, Process> newProcs, int newKey)
    {
        foreach (Process process in newProcs.Values)
        {
            if (process.Id == newKey)
                return true;
        }

        return false;
    }
    private static void checkProcesses(Dictionary<string, Process> newProcs)
    {
        foreach (string currProc in newProcs.Keys)
        {
            bool processExists = false;
            foreach (Process process in Process.GetProcesses())
            {
                if (process.Id == newProcs[currProc].Id)
                {
                    processExists = true;
                    break;
                }

            }
            if (!processExists)
            {
                Console.Write("Process Finished: ");
                Console.WriteLine(currProc);
                newProcs.Remove(currProc);
                if (newProcs.Count == 0)
                    break;
            }
        }
    }
}

任何想法?

推荐答案

U可以创建这样一个方法

u can create a method like this

 public static void Thread1() {
           //paste your main code hear
            }

复制和过去的所有code,写入主
这种新方法

copy and past all your code, written in the main to this new method.

主要改变了一些这样的

 public static void Main() {
            Console.WriteLine("Before start thread");
            Thread tid1 = new Thread(new ThreadStart(Thread1 ) );
            tid1.Start();                
    }

这篇关于不断监视进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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