Click here to Skip to main content
15,908,166 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can Restart console application and windows service when exception occur

What I have tried:

how can Restart console application and windows service when exception occur
Posted
Updated 14-Apr-16 1:10am
Comments
Member 12003400 14-Apr-16 5:07am    
you can manage with try and catch. Handle exception in catch properly and you can define what to proceed if exception occurs.
.net developer123456789 14-Apr-16 5:25am    
try
{
logic here
}
catch (Exception ex)
{
//System.Runtime.InteropServices.COMException
WriteToErrorFile("ReadMail Method Error on: {0} " + ex.Message);
How can restart the Console app?
}
Member 12003400 14-Apr-16 5:43am    
call that function again which you are calling in main method.
.net developer123456789 14-Apr-16 5:46am    
Yes,but my case stop application,now how can restart app ?

1 solution

When your application has terminated it can be only restarted by some kind of watcher.

The simplest solution would be using a batch / command file that re-starts the application when it returns specific codes. For example return zero from your main function and call Environment.Exit Method (Int32) (System)[^] with an exception specific value from within exception handlers that terminate the application (it might be also necessary to provide a handler for unhandled exceptions).

The command file might look like:
@ECHO OFF
:Restart
myapp.exe
IF %ERRORLEVEL% NEQ 0 GOTO Restart

This will restart myapp.exe when it returns non zero.

Testing the ERRORLEVEL variable can be used to check for different codes. To check only for zero the command file might be simpler:
@ECHO OFF
:Restart
myapp.exe || GOTO Restart
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900