I have some code inside a background worker DoWork event which check if a process is running or not. The code in the DoWork does the following:
-> If the process is not running, check again twice
-> If the process is running, check if it is responding
-> If the process is running and respoding, do nothing
-> If the process is running and not responding, restart the process
void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
int numberTrials = 0;
bool isNotRunning = false;
while (isNotRunning = (someProcess.Length == 0) && numberTrials < 3)
{
Debug.WriteLine(String.Format("numTrial = {0}", numberTrials.ToString()));
Thread.Sleep(3000);
++numberTrials;
}
if (isNotRunning)
{
Debug.WriteLine("Start Task");
someProcess.Start();
}
else
{
if(!someProcess.IsKioskResponding)
{
Debug.WriteLine("Kill Task");
}
}
}
The above thing runs fine but I have to loop the above DoWork task for every 3 minutes. Would it be a good thing to keep the above looping task in a timer that has an interval of 3 minutes? (In that way I would have to take care of Thread.Sleep x numberTrials not to exceed the Timer’s interval). Any thoughts on this?
Have you also had a look at the Process.WaitForExit Method
It is explained with an example here: process restart loop