c# - How to scan and kill a process by name -


i make program scan , kill process name. found this:

foreach (process process in process.getprocessesbyname("vlc")) {     process.kill();     process.waitforexit(); } 

the problem kills process 1 time , closes. want program continue , kill process again if starts again. ideas?

i imagine use such solution:

private managementeventwatcher watchforprocessstart(string processname) {     string querystring =         "select targetinstance" +         "  __instancecreationevent " +         "within  10 " +         " targetinstance isa 'win32_process' " +         "   , targetinstance.name = '" + processname + "'";      // dot in scope means use current machine     string scope = @"\\.\root\cimv2";      // create watcher , listen events     managementeventwatcher watcher = new managementeventwatcher(scope, querystring);     watcher.eventarrived += processstarted;     watcher.start();     return watcher; } 

and in case when new process started:

private void processstarted(object sender, eventarrivedeventargs e) {   //here kill process. } 

but pretty weird concept kill process every time starts. rather try find out way prevent starting don't know business case of course.

you can find more on managementeventwatcher class here.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -