I have created a console application in C#. How can I program this application so that it will re-start itself after a crash?
-
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
-
2Have 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 Answers
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;
}
}
}

- 258,201
- 41
- 486
- 479

- 788
- 5
- 7
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?
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
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

- 1
- 1

- 3,897
- 5
- 31
- 42
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.

- 9,529
- 8
- 68
- 159
-
1I would prefer to handle AppDomain.CurrentDomain.UnhandledException events instead of this solution. So you can use try/catch in situations you can recover from.. – Peter Bons Sep 21 '16 at 13:04
-
This will not work in case of some exceptions like stack overflow or out of memery exceptions – omer schleifer Sep 21 '16 at 13:04
-
What's the point in physically exiting and restarting in the case of an exception? – ThePerplexedOne Sep 21 '16 at 13:04
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.

- 2,400
- 2
- 29
- 32