I basically need to run this code every 5 seconds or so:
if (oneperone.HasExited)
{
oneperone.Start();
}
foreach (Process p in System.Diagnostics.Process.GetProcessesByName("WerFault"))
{
try
{
p.Kill();
p.WaitForExit(); // possibly with a timeout
}
catch (Win32Exception winException)
{
// process was terminating or can't be terminated - deal with it
}
catch (InvalidOperationException invalidException)
{
// process has already exited - might be able to let this one go
}
}
I’ve tried using a timer with the example here: http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx and I’ve tried running an infinite loop with system thread wait commands but both give unacceptable performance and the system wait command obviously locks up the thread I need to do other things in between the 5 second iteration
keep in mind the 5 seconds thing is arbitrary it can be longer or shorter I just need to keep repeating this code but efficiently hopefully someone has a graceful solution?
There are likely better way to achieve what you need, but here is suggestion for what you want:
Thread.Sleep will give you low CPU usage as long as you don’t do long end expensive operations on each iteration.
Create new thread with Sleep calls and queue up processes to kill in some queue (to avoid long operations on this thread and duplication of “kill requests”); kill processes from the queue on separate thread/worker. Avoid attempting to kill the same process more than once.