4

I have a small, long-running, embedded, NETMF program that stops for unknown reasons (see How do we trouble shoot a long-running NETMF program that stops in production?). As a workaround, I would like it to automatically restart on fail. My sense is that a try-catch block could do this. How? Is this even possible? Is there a better approach?

public class Program
{
    private static Timer timer;      
    public static void Main()
    {
        Program p = new Program();

        Program.timer = 
            new Timer(this.TimerCallback_DoSomething, new object(), 0, 1000);

        Thread.Sleep(Timeout.Infinite);
    }

    private void TimerCallback_DoSomething(object stateInfo)
    {
        // do something
    }
}
Community
  • 1
  • 1
Shaun Luttin
  • 107,550
  • 65
  • 332
  • 414
  • 1
    If you are running this as a service, you can have the service automatically restart itself on failure. – David Apr 08 '14 at 20:42
  • For the most stable approach this should be done as a DOS batch file that restarts the program if it exists with a non-zero result. – Reactgular Apr 08 '14 at 20:44
  • @PhatWrat It's an embedded NETMF program, so I think a service is not possible. I updated my question to emphasize that. – Shaun Luttin Apr 08 '14 at 20:47

5 Answers5

3

There are some good suggestions in this thread.

Detecting and restarting my crashed .NET application

For example, you can register for the Application.ThreadException event, and then handle the application restart using the application recovery APIs (requires P/Invoke).

http://community.bartdesmet.net/blogs/bart/archive/2006/11/11/Windows-Vista-Application-Recovery-with-C_2300_.aspx

[MSDN] Understanding Application Recovery and Restart

Community
  • 1
  • 1
Trevor Sullivan
  • 19,718
  • 7
  • 82
  • 121
  • If you have found a duplicate question you should flag/vote to close as a duplicate, not post a link to the duplicate in an answer. – Servy Apr 08 '14 at 20:42
  • Unreliable, this does not necessarily start. Without knowing what goes on - that may simply not trigger. – TomTom Apr 08 '14 at 20:42
  • 1
    I would have made one of the same suggestions as Marc Gravell from that question - load the application within a separate AppDomain and monitor its shutdown. It wouldn't require an extra process, so should be applicable in most scenarios. – Snixtor Apr 08 '14 at 20:56
2

Create a Windows Service: http://visualstudiomagazine.com/articles/2013/10/01/easily-create-windows-services-with-topshelf.aspx

And set it to restart on failure: How can a windows service programmatically restart itself?

Community
  • 1
  • 1
vtortola
  • 32,045
  • 25
  • 144
  • 246
1

Whether you could do this in the same application via a simple try/catch depends on what sort of exception is thrown. I'll take the worst case scenario and say that the app is irrecoverably lost.

Have a second application that restarts the first when it stops. In rough code:

Process p = new Process();
p.Exited += StartItAgain;
// other setup
p.Start();
Tim S.
  • 52,076
  • 7
  • 84
  • 114
  • My understanding is that NETMF can have only one application running at a time. I appreciate that this wasn't clear from the original question and have updated it. – Shaun Luttin Apr 08 '14 at 20:50
1

If you don't know what's causing the app to crush, meaning you can't deal with an exception you probably need a windows service to re-lanch your app.

Pantelis
  • 2,020
  • 3
  • 23
  • 39
1

I agree with Pantelis in .Net you have to handle your exceptions probably, if you do not know where the error coming from then you have to use UnhandledException to work with the Problem.

I do not think restart the application will solve your Problem it will hide your Problem, but if you like to do that there are a lot of ways to do that:

  • In try catch start the process again.
  • In UnhandledException start the application.
  • Error code on exception and when application exist Event check the error code and if error code not 0 then restart the application.
Bassam Alugili
  • 13,927
  • 7
  • 49
  • 67