I'm testing a simple Windows Service that detects if an application is responding or not and writes the result to a file.
During debug everything works fine but once I publish the service it will not detect the application as hung? It still writes to the file without issue but for some reason the responding check is not firing. Seems like I'm overlooking something?
//Start method
protected override void OnStart(string[] args)
{
var worker = new Thread(DoWork);
worker.Name = "MyWorker";
worker.IsBackground = false;
worker.Start();
}
//Do Work
void DoWork()
{
string curFile = AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt";
Process[] runningProcs = Process.GetProcessesByName("TestApp");
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt"))
{
File.Delete(curFile);
}
while (runningProcs.Length > 0)
{
System.Threading.Thread.Sleep(2500);
foreach (Process checkProc in runningProcs)
{
if (checkProc.Responding)
{
File.AppendAllText(curFile, "Application is Responding @ " + DateTime.Now + System.Environment.NewLine);
}
else
{
File.AppendAllText(curFile, "Application is Not Responding @ " + DateTime.Now + System.Environment.NewLine);
}
}
}
}
The debug output:
Application is Responding @ 1/15/2014 8:59:40 AM
Application is Responding @ 1/15/2014 8:59:43 AM
Application is Responding @ 1/15/2014 8:59:45 AM
Application is Not Responding @ 1/15/2014 8:59:53 AM
Application is Not Responding @ 1/15/2014 8:59:55 AM
Application is Not Responding @ 1/15/2014 8:59:58 AM
Service output:
Application is Responding @ 1/15/2014 9:03:02 AM
Application is Responding @ 1/15/2014 9:03:05 AM
Application is Responding @ 1/15/2014 9:03:07 AM
Application is Responding @ 1/15/2014 9:03:10 AM
Application is Responding @ 1/15/2014 9:03:12 AM