views:

244

answers:

6

My process sometimes throws exception like dllnotfound after start. i have a monitor service responsible for maintaining the consistent state of the process.

how can i keep track of state of my process using windows service.

is there an open source implementation of windows service that maintains/track the state of process in windows.

+2  A: 

Edit: So the actual problem wasn't that the process was dying, but that the process was stuck in an exception handler dialog waiting for the user to hit debug or cancel. The solution to the problem was to disable the .net JIT debug dialog, instructions here http://weblogs.asp.net/fmarguerie/archive/2004/08/27/how-to-turn-off-disable-the-net-jit-debugging-dialog.aspx

My original proposed solution is below


Not a window service, but this is a pretty easy .NET program to write.

use System.Diagnostics.Process to get a Process object for the process you want to check. You can use GetProcessByName if you want to open an existing process. If you create the process from C#, then you will already have the process object.

Then you just can WaitForExit either with or without a timeout on the Process object. or test the HasExited property, or register an Exited callback. Once the process has exited, you can check the ExitCode property to find out whether the process returned an error value.

John Knoeller
yes i started the process from c#. when the process throws an exception, its state is running, i check for hasexited() property every 10 secs but the state is not exited. the process throws dllnotfound exception and asks me weather i want to start a debugger.
Kazoom
Do you have the code for the target process? You will need to execute code _in the target process_ if you want to do anything with the exception _before_ it causes the process to exit.
John Knoeller
changing target process code would be my last resort, is there some setting that would prevent windows to give a pop up message box when a process throws exception like dllnotfound. if i can figure out someway to dismiss the popupbox automatically once an error comes the process dies, otherwise its keeps running
Kazoom
@Kazoom: you can disable the debugger http://msdn.microsoft.com/en-us/library/5hs4b7a6(VS.80).aspx but this change will affect all processes, not just the one you care about.
John Knoeller
thanks that did not work but it seems i have found another similar solution http://bit.ly/9lj4vG
Kazoom
+2  A: 

That's not possible, exceptions are local to a thread first, local to a process secondary if it is unhandled. An unhandled exception will terminate the process. The only shrapnel you could pick up from such a dead process is the process exit code. Which should be set to 0xe0434f4e, the exception code for an unmanaged exception. No other relevant info is available, unless there's an unhandled exception handler in the process that logs state. That state is very unreliable, the process suffered a major heart attack.

Keeping multiple processes in synch and running properly when they may die from exceptions is extraordinarily difficult. Only death can be detect reliably, avoid doing more.

Hans Passant
yes but if dll is not found the process pops up a message box and waits for user to dismiss it. the state of the process meanwhile is running, is it possible to make the process not popup the message box like it does and die.
Kazoom
Does it really matter? It isn't ever going to work properly unless somebody puts the DLL in the right place. Somebody will after they clicked okay 10 times or so. Anyhoo, maybe SetErrorMode() to suppress the message box.
Hans Passant
dllnotfoundexception is just an example, i dont want the process to get stuck and the monitor thinking that it is still running, if process encounters exception it should just die, so monitor can restart a new instance of that process. the whole process/monitor runs without user intervention. there would be no one to click ok :(
Kazoom
You'll need to come with a better example. But whenever the process decides to put up a message box, it is going to take somebody to click it. If this is not desired then you should *really* consider not using multiple processes.
Hans Passant
A: 

One thing you could do is to monitor the CPU usage of the process. I am assuming your process goes away when the exception is thrown. Therefore, the CPU usage of the process should be 0 since it is no longer available. Therefore, if the CPU usage stays at zero for a certain period of time, you can safely assume that the process has raised the exception. This method is not fool proof since you are basing your decision on CPU usage and a legitimate process may have a zero CPU usage for a given period of time. You can incorporate this check inside your monitoring service or you could write a simple VB script to check process CPU usage externally.

naivnomore
Would you care to explain the reason for the down vote?
naivnomore
A: 

In the case of DllNotFoundException and other things that happen at startup, you can have the application indicate when it's finished starting up. Have it write a timestamp to a file, for instance. Your monitor can compare the time the application started with the time in the file.

John Saunders
+1  A: 

Have your process write events and exceptions to the system's application log and have your monitor check for entries periodically to find events relating to your process, and you can check the system events for service start and stop events.

If the process itself is a windows service, you can check its status using the `System.ServiceProcess.ServiceController'.

Jay