0

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

Justin
  • 84,773
  • 49
  • 224
  • 367
bidak
  • 21
  • 7

7 Answers7

9

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

Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • 4
    +1: For the advice of first trying to figure out **why** it crashes! – Hemant Oct 04 '10 at 10:23
  • when I say crashes maybe, unhandled exception !! or windows crash and reboot, I have to be sure that my application is always running – bidak Oct 04 '10 at 12:40
  • @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 – Iain Ward Oct 04 '10 at 12:46
6

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
  • 3,509
  • 3
  • 39
  • 45
  • 1
    Just don't forget that this requires at least Vista/Windows Server 2008. – Dirk Vollmar Oct 04 '10 at 10:27
  • I have win XP right now , but if there is an extern problem (windows crash) how can my application restart (after windows reboot) ? – bidak Oct 04 '10 at 12:42
  • For older versions of windows you can have your own seconary exe monitoring the primary application. – zproxy Oct 10 '10 at 12:23
3

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.

Justin
  • 84,773
  • 49
  • 224
  • 367
1

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
  • 156
  • 3
  • 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 Oct 04 '10 at 12:50
  • You can use ants to detect leak of memory: http://www.red-gate.com/products/ants_memory_profiler/index.htm?utm_source=simpletalk&utm_medium=article&utm_content=mikebloise&utm_campaign=antsmemoryprofiler – Ali YILDIRIM Oct 04 '10 at 13:03
0

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
  • 28,510
  • 21
  • 92
  • 151
0

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.

vamyip
  • 1,161
  • 1
  • 10
  • 35
  • 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 Oct 06 '10 at 09:05
0

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
  • 21
  • 7