Reputation: 1077
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
Upvotes: 0
Views: 1337
Reputation: 2433
As per the property's documentation, Process.Responding gets a value indicating whether the user interface of the process is responding or not. I expect that under the covers, it is calling EnumWindows() or EnumThreadWindows() to find the windows associated with the process, then calling SendMessageTimeout() on each window to see if one responds.
Unfortunately this presents a problem for your Windows Service, which, due to Session 0 Isolation, is not running in the same session as the process you wish to check. The service's call to EnumWindows() will only return windows in Session 0, none of which will be for your application running in Session 1 or above. And finding no windows is enough for Responding to return 'true' since it knows no better.
Things work when debugging because your application and GUI are running in the same session and EnumWindows() finds the window as expected.
Upvotes: 1