views:

137

answers:

7

Hi all,

I created a winform (monitoring) application using VS 2005 (c#), and now, I have a problem when this application crashes for some reason, I have to be sure that it will be restarted automatically.

How can I resolve this? (maybe by using windows services application?)

Thanks

+5  A: 

Consider this:

http://msdn.microsoft.com/en-us/library/cc303699.aspx

[DllImport("kernel32.dll")]
public static extern int RegisterApplicationRestart(
    [MarshalAs(UnmanagedType.BStr)] string commandLineArgs,
    int flags);

Minimum supported server

Windows Server 2008

http://msdn.microsoft.com/en-us/library/aa373347(VS.85).aspx

zproxy
Just don't forget that this requires at least Vista/Windows Server 2008.
0xA3
I have win XP right now , but if there is an extern problem (windows crash) how can my application restart (after windows reboot) ?
bidak
For older versions of windows you can have your own seconary exe monitoring the primary application.
zproxy
+9  A: 

Yes, a creating a Windows Service would work as you can set it to automatically restart if it crashes but a better way would be to prevent it crashing in the first place! Is there a specific reason it crashes?

With good error handling and reporting you can write it so that it simply reports any errors that occur and carries on, which IMHO would be the best route to go

w69rdy
+1: For the advice of first trying to figure out **why** it crashes!
Hemant
when I say crashes maybe, unhandled exception !! or windows crash and reboot, I have to be sure that my application is always running
bidak
@bidak: Use Try/Catches to cater for unhandled exceptions, and to start your app after a reboot put a shortcut to it in the 'Startup' folder of your Start Menu
w69rdy
+3  A: 

Creating a Windows service is a very good idea for any long-running background process for many reasons, however re-starting a crashed application is not one of them!

You should work out why the application is crashing and prevent it from happening.

By all means, also convert your application to a Windows service - you will see many benefits, however the correct way to solve your problem is to fix the application crash in the first place.

Kragen
how to convert my application to a Windows service ?
bidak
@bidak - w69rdy's post has an excellent link.
Kragen
A: 

Since you say that you use a Windows Forms application you cannot use a Windows Service for that, since a Windows Service is not allowed to have a GUI.

What I would do it that I would create an invisible "watchdog" application which monitors the process and automatically restarts it when it crashes.

codymanix
A: 

Hi,

You can put a try catch block around the code that is most likely causing the crash. Then write the exception message to a log file. You can also set a debug point in the catch block to see other details like call stack,etc.

Regards,
Vamyip

vamyip
Thanks you all, the solution I choose is : in the main program I add an exception events (UnhandledExceptionEventHandler )
bidak
A: 

For*strong text* a watcher app. You should create a timer on the windows service and code something like this in the timer tick event:

        Process[] procs = Process.GetProcessesByName("you app name");
        if (procs.Length == 0)
            Process.Start("your app filename");

if you really cant do anything about the crash problem i would recommend a try-catch instead of a watcher. (Dont forget to re-throw handled major exceptions)

    [STAThread]
    static void Main()
    {
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        catch(Exception ex)
        {
            //log the exception here
            Application.Restart();
        }
    }
Ali YILDIRIM
I added this to my application :Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);in these function I restart my application, but if it's a windows probleme (leak of memory,...) I can't detect that in my application that was fired by windows system. Thanks I will see for windows service example.
bidak
Ali YILDIRIM
A: 

Thanks you all, the solution I choose is : in the main program I add an exception events (UnhandledExceptionEventHandler & ThreadExceptionEventHandler see above) in these events I restart the program (also putting log & email to trace errors). And for the reboot problem I add registry key in [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] with my application path to be sure that my application will be restarted after the windows reboot ;)

bidak
You can mark the most appropriate post as an answer. It will improve your acceptance rate and encourage more users to answer your future questions. Thanks
vamyip