简体   繁体   English

进程的 C#.NET 监控

[英]C#.NET Monitoring for a Process

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.我们有这个第 3 方内部应用程序,它有一些小问题,目前,在我们的 Citrix 环境中修复之前,我需要密切关注它并在运行时间过长时终止该进程。 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.我想要一个服务来监视和检测它,然后在它运行时间过长时终止它。

So I started a Windows Service project in Visual Studio and I found this code from CodeProject which registers with WMI using ManagementEventWatcher:所以我在 Visual Studio 中启动了一个 Windows 服务项目,我从 CodeProject 中找到了这段代码,它使用 ManagementEventWatcher 向 WMI 注册:

        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();

The problem with this code is that where it says "this.OnEventArrived", I get the following error:这段代码的问题在于它说“this.OnEventArrived”,我收到以下错误:

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?)错误 1“MyServiceApp.Service1”不包含“OnEventArrived”的定义,并且找不到接受“MyServiceApp.Service1”类型的第一个参数的扩展方法“OnEventArrived”(您是否缺少 using 指令或程序集引用?)

What's the deal?这是怎么回事?

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

OnEventArrived should look like this. OnEventArrived 应该是这样的。

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

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.您可能想阅读有关 WMI 的更多信息,看看是否有更好的方法。 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.退出时,它将打印 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM