C#.NET Monitoring for a Process -
we have 3rd party in house app that's little buggy , now, till it's fixed, in our citrix environment, need keep eye on , kill process if runs long. able poll , kill if running that's quite dirty , require me use scheduled task. want service monitor , detect kill if it's running long.
so started windows service project in visual studio , found this code codeproject registers wmi using managementeventwatcher:
string pol = "2"; string appname = "myapplicationname"; string querystring = "select *" + " __instanceoperationevent " + "within " + pol + " targetinstance isa 'win32_process' " + " , targetinstance.name = '" + appname + "'"; // replace dot machine name watch machine string scope = @"\\.\root\cimv2"; // create watcher , start listen managementeventwatcher watcher = new managementeventwatcher(scope, querystring); watcher.eventarrived += new eventarrivedeventhandler(this.oneventarrived); watcher.start(); the problem code says "this.oneventarrived", following error:
error 1 'myserviceapp.service1' not contain definition 'oneventarrived' , no extension method 'oneventarrived' accepting first argument of type 'myserviceapp.service1' found (are missing using directive or assembly reference?)
what's deal?
the documentation can found on msdn https://msdn.microsoft.com/en-us/library/system.management.managementeventwatcher.eventarrived%28v=vs.110%29.aspx
oneventarrived should this.
private void oneventarrived(object sender, managementeventargs args) { //do work here } here sample program monitor notepad. want read more on wmi see if there better way. can launch notepad via start menu , see notepad started out put console. on exiting print notepad exited. not know messages can output.
static void main(string[] args) { string pol = "2"; string appname = "notepad.exe"; string querystring = "select *" + " __instanceoperationevent " + "within " + pol + " targetinstance isa 'win32_process' " + " , targetinstance.name = '" + appname + "'"; // replace dot machine name watch machine string scope = @"\\.\root\cimv2"; // create watcher , start 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); }
Comments
Post a Comment