-2

I have a application that needs to run all day but sometimes it gives a exception and then the application crashes. If that happends the devices connected to the application cannot do their work.

What I need to do is restart the application when I crashes but I dont want to make another program to do that.

This is what is what I think I need to do:

static void Main(string[] args)
    {
        try
        {
            if (!Environment.UserInteractive)
            {
                ServiceBase.Run(new WebDaemonService(HttpClass.StartListening));
            }
            else
            {
                HttpClass.StartListening();
            }
        }
        catch(Exception ex)
        {
            Restart();
        }               
    }

But how do I make a restart function, do I just call the main class again or do I need to do something else.

The if else in the try is to check if the application needs to start as a windows service or as a application.

Can someone explain to me how I can make a restart function?

Gusdor
  • 14,001
  • 2
  • 52
  • 64
Collin
  • 914
  • 1
  • 9
  • 30
  • 1
    You should try handling/fixing the exception instead... catching the exception as part of your programs flow isn't a great way to do things – Sayse Aug 17 '15 at 06:41
  • @SayseHow can i to that so the application will run again – Collin Aug 17 '15 at 06:41
  • I have no idea, you haven't said what the exception *is* – Sayse Aug 17 '15 at 06:42
  • 1
    The use of ServiceBase.Run(..) indicates that it can run as a Windows Service ? - Under Computer Management => Services and Applications => Services => . Look at the tab "Recovery". Where you can take actions it the program fails – rdyhalt Aug 17 '15 at 06:42
  • @togocoder thanks for the tip i will look into righyt now. – Collin Aug 17 '15 at 06:43
  • This is the exception: An operation was attempted on a nonexistent network connection – Collin Aug 17 '15 at 06:45
  • @CKY I think you're asking the wrong question. Seems like you're already working on a workaround while you could have asked a question to solve the real problem. Restarting the program is only a last resort solution which I think is not needed. – huysentruitw Aug 17 '15 at 06:52
  • Yes the problem is that when the application crashes i need to start it manualy. I want the application to do it by itself – Collin Aug 17 '15 at 06:57

1 Answers1

0

Add an event handler on ProcessExit event of current domain.

AppDomain.CurrentDomain.ProcessExit

See doc at MSDN

Restart you application or service from there. If this is windows service, you can set it to restart on crashes from Services.msc window also.

Code sample:

class Foo
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); 
       // Do your stuffs
    }

    static void CurrentDomain_ProcessExit(object sender, EventArgs e)
    {
        Process p = new Process();
        p.StartInfo.FileName = "Full path of exe";
        p.StartInfo.Arguments = "arguments";
        p.Start();
    }
}
Sarvesh Mishra
  • 2,014
  • 15
  • 30
  • Yes i understand that, but how can i start my application and where do i need to do this? – Collin Aug 17 '15 at 06:51
  • So `AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); ` will be above my code? that i already have? – Collin Aug 17 '15 at 07:05
  • It should be at that line which must get called once. So if you make it as first statement of Main(), it would be called before any crashes occur in program. – Sarvesh Mishra Aug 17 '15 at 07:08
  • Please read the documentation and its remarks carefully. do not perform much task in CurrentDomain_ProcessExit() – Sarvesh Mishra Aug 17 '15 at 07:10