Question

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

Was it helpful?

Solution 6

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 ;)

OTHER TIPS

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

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

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.

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();
        }
    }

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top