-2

I have created a console application in C#. How can I program this application so that it will re-start itself after a crash?

tedi
  • 6,350
  • 5
  • 52
  • 67
ruud
  • 210
  • 4
  • 12
  • You run it. If it disappears unexpectedly it crashed.:-). But seriously, what do you mean? – Peter Bons Sep 21 '16 at 13:00
  • You can start is from another application using System.Diagnostics, or you could start it using Windows Task Scheduler. We need some more information to be more helpful – Shannon Holsinger Sep 21 '16 at 13:00
  • 2
    Have your console app return a "success" exit code other than zero. Then write a launcher app to start it, and wait for it to exit. If the exit code isn't the special "success" code, start the console app again. – Matthew Watson Sep 21 '16 at 13:01

4 Answers4

4

If I understand your question correctly, you want to attempt to re-start a console app in the event of a crash. In C# console-apps the method defined as the entry point (usually static void main) is the root of the call stacks in the app. You essentially would need to call that method recursively. You will want to make sure that the app eventually fails if it is in some unintended or unrecoverable state.

For example in the main class:

static int retryCount;
const int numberOfRetries = 3;
static void Main(string[] args)
{
    try
    {
        var theApp = new MyApplicationType(args);
        theApp.StartMyAppLogic();
    }
    catch (ExpectedExceptionType expectThisTypeOfException)
    {
        thisMethodHandlesExceptions(expectThisTypeOfException);
    }
    catch (AnotherExpectedExceptionType alsoExpectThisTypeOfException)
    {
        thisMethodHandlesExceptions(alsoExpectThisTypeOfException);
    }
    catch (Exception unexpectedException)
    {
        if(retryCount < numberOfRetries)
        {
            retryCount++;         
            Main(args);
        }
        else
        {
            throw;
        }
    }
}
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
RJ Programmer
  • 788
  • 5
  • 7
2
  1. You can use a watchdog to process your monitor and restart it if crashed: see: What's the best way to watchdog a desktop application?

  2. You can use a windows service instead and set it's recovery options as indicated here: https://serverfault.com/questions/48600/how-can-i-automatically-restart-a-windows-service-if-it-crashes

  3. You can use a scheduled task in task manager to start your application periodically , and set it to only start if previous run has ended: https://support.microsoft.com/en-us/kb/323527

Community
  • 1
  • 1
omer schleifer
  • 3,897
  • 5
  • 31
  • 42
0

You could try something like this:

static void Main(string[] args)
{
    try
    {
        // Application code goes here
    }
    catch (Exception)
    {
        var applicationPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        Process.Start(applicationPath);
        Environment.Exit(Environment.ExitCode);
    }
}

Basically, wrap all the code in a try/catch, and if any exceptions occur, the program will retrieve the .exe location with System.Reflection.Assembly.GetExecutingAssembly().Location; and then call Process.Start to run the application again.

Bassie
  • 9,529
  • 8
  • 68
  • 159
-1

You should control your console app from another application (watchdog, sheduler, procmon, servman, ...).

E.g. you can create your console app as a service and control it from service manager.

honzakuzel1989
  • 2,400
  • 2
  • 29
  • 32