Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am developing a windows form application for a client and they require that even if the application is stopped due to some unhandled exception or closed it should pop up again and keep running. I am a beginner with C# and would highly appreciate any help
Posted

Good idea! I think all applications should implement it. This is extremely easy to do.

You need to do two things:

  1. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    You need to do it before the call to Application.Run. Therefore, it's the best to do it in your entry point (Main) method. Please see:
    http://msdn.microsoft.com/en-us/library/ms157905.aspx[^],
    http://msdn.microsoft.com/en-us/library/system.windows.forms.unhandledexceptionmode.aspx[^].
  2. When the above is done, you need to add a universal handle for unhandled exceptions of the UI thread. A good place to do it is in the very beginning of your main form class constructor. Handle this event:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.application.onthreadexception.aspx[^].

    If you do it, all exceptions thrown inside main event loop of the UI will be caught by your handler. You can handle the exception (show dialog box, for example) and continue execution.


Try it out — it will work immediately.

[EDIT]

See also my past answer where you can find more detail: How do i make a loop that will stop when a scrollbar reaches the bottom[^].

—SA
 
Share this answer
 
v2
Comments
Joezer BH 29-Jan-13 4:06am    
That's a nice piece Sergey, however, he basically asked for a Keep-Alive methodology, the header is mis-leading.
Sergey Alexandrovich Kryukov 29-Jan-13 15:55pm    
I'm not sure. Look at the body of the question, not title. As it is formulated, is is resolved exactly by the technique I described.
—SA
Adam R Harris 29-Jan-13 16:23pm    
Great answer, you beat me to it.
My 5.
Sergey Alexandrovich Kryukov 29-Jan-13 16:27pm    
Thank you, Adam.
—SA
Andreas Gieriet 29-Jan-13 16:34pm    
I think once an unhandled exception is caught at top level (e.g. with the means you describe above), the status of the application is unknown. You can not "jump back" to the location where the exception was thrown, correct?

I've never done this, but at that point it may be best to restart the app (e.g. by either fork in the exception handler a new process and let the current process die, or by (re-)loading the executable into this app domain, see AppDomain.Load()).
Cheers
Andi
The best way is to have a separate application which looks to see if the program is running - if it isn't then run it. If it checks every second or so, it shouldn't have too much overhead, and the user won't notice the gap.

If you look at this: A simple way to ensure only one instance is running.[^] and just reverse the logic you will know if it is running or not.
 
Share this answer
 
Comments
Joezer BH 29-Jan-13 4:07am    
Actually you didn't answer his questions since in your Tip, the other instances are being handled within the one instance you wish to keep.
The problem he's talking about is a crash of the process.
He needs some other process to raise the application again, crash-recovery.
OriginalGriff 29-Jan-13 4:18am    
That's why I said "and just reverse the logic".
Running the app if it isn't there is a trivial exercise!
Process.Start("...");
Will do it :laugh:
Joezer BH 29-Jan-13 4:21am    
The thing is that the dude trying to figure out what to do with our replies, is not always skilled enough to do the math yet.

but you get my 5+ (both for the answer as well as for the Tip)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900