进程的 C#.NET 监控 [英] C#.NET Monitoring for a Process

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

问题描述

我们有这个 3rd 方的内部应用程序,它有一些小问题,目前,在我们的 Citrix 环境中修复之前,我需要密切关注它并在运行时间过长时终止该进程.如果它正在运行,我能够轮询它并杀死它,但这很脏,需要我使用计划任务.我想要一个服务来监视和检测它,然后在它运行时间过长时终止它.

We have this 3rd party in house app that's a little buggy and for now, till it's fixed, in our Citrix environment, I need to keep an eye on it and kill the process if it runs too long. I was able to poll for it and kill it if it was running but that's quite dirty and would require me to use a scheduled task. I want a service to monitor and detect it then kill it if it's running too long.

于是我在 Visual Studio 中启动了一个 Windows 服务项目,我发现 CodeProject 中的代码 使用 ManagementEventWatcher 向 WMI 注册:

So I started a Windows Service project in Visual Studio and I found this code from CodeProject which registers with WMI using ManagementEventWatcher:

        string pol = "2";
        string appName = "MyApplicationName";

        string queryString =
            "SELECT *" +
            "  FROM __InstanceOperationEvent " +
            "WITHIN  " + pol +
            " WHERE TargetInstance ISA 'Win32_Process' " +
            "   AND TargetInstance.Name = '" + appName + "'";

        // You could replace the dot by a machine name to watch to that machine
        string scope = @"\\.\root\CIMV2";

        // create the watcher and start to listen
        ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
        watcher.EventArrived += new EventArrivedEventHandler(this.OnEventArrived);
        watcher.Start();

此代码的问题在于,它显示this.OnEventArrived"时,出现以下错误:

The problem with this code is that where it says "this.OnEventArrived", I get the following error:

错误 1MyServiceApp.Service1"不包含OnEventArrived"的定义,并且找不到接受MyServiceApp.Service1"类型的第一个参数的扩展方法OnEventArrived"(您是否缺少 using 指令或程序集参考?)

Error 1 'MyServiceApp.Service1' does not contain a definition for 'OnEventArrived' and no extension method 'OnEventArrived' accepting a first argument of type 'MyServiceApp.Service1' could be found (are you missing a using directive or an assembly reference?)

这是怎么回事?

推荐答案

相关文档可以在 MSDN 上找到https://msdn.microsoft.com/en-us/library/system.management.managementeventwatcher.eventarrived%28v=vs.110%29.aspx

The documentation for this can be found on MSDN https://msdn.microsoft.com/en-us/library/system.management.managementeventwatcher.eventarrived%28v=vs.110%29.aspx

OnEventArrived 应如下所示.

OnEventArrived should look like this.

private void OnEventArrived(object sender, ManagementEventArgs args)
{
//do your work here
}

这是一个监视记事本的示例程序.您可能想阅读有关 WMI 的更多信息,看看是否有更好的方法.您可以通过开始菜单启动记事本,您将看到记事本开始输出到控制台.退出时,它将打印 Notepad Exited.我不知道所有可以输出的消息.

Here is a sample program that will monitor notepad. You probably want to read more on WMI to see if there is a better way. You can launch notepad via the start menu and you will see Notepad started out put to the console. On exiting it will print Notepad Exited. I do not know all the messages that can be output.

    static void Main(string[] args)
    {

        string pol = "2";
        string appName = "Notepad.exe";

        string queryString =
            "SELECT *" +
            "  FROM __InstanceOperationEvent " +
            "WITHIN  " + pol +
            " WHERE TargetInstance ISA 'Win32_Process' " +
            "   AND TargetInstance.Name = '" + appName + "'";

        // You could replace the dot by a machine name to watch to that machine
        string scope = @"\\.\root\CIMV2";

        // create the watcher and start to listen
        ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
        watcher.EventArrived += new EventArrivedEventHandler(OnEventArrived);
        watcher.Start();
        Console.Read();
    }

    private static void OnEventArrived(object sender, EventArrivedEventArgs e)
    {
        if (e.NewEvent.ClassPath.ClassName.Contains("InstanceCreationEvent"))
            Console.WriteLine("Notepad started");
        else if (e.NewEvent.ClassPath.ClassName.Contains("InstanceDeletionEvent"))
            Console.WriteLine("Notepad Exited");
        else
            Console.WriteLine(e.NewEvent.ClassPath.ClassName);
    }

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

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