tags:

views:

156

answers:

6

Hello there, any ideas of how to auto-restart (restart) Console application in .NET ?

I know that forms application have something like this:

Application.Restart();

but this is not possible for Console.

Also, NOTE: The problem is, that I can't start two instances of the same app :)

A: 

You could create a new process for your application (see the Process class), initiate it, and close your current application immediately after that.

Konamiman
The problem is, that I can't start two instances of the same app :)
Lukas Šalkauskas
Is that so that they don't run concurrent tasks? If so, you could add in a simple while(processStillRunning) Thread.Sleep(1000); until the other application has successfully closed. This can also be done via the Process class.
Kyle Rozendo
You could use this, but then block the new process just inside the main method, until the old process has closed, to ensure that two processes do not run at the same time.
Kazar
A: 

Well you can start a program that simply starts new instance of your program after old instance exits.

grega g
A: 

You can create a launcher application, that would wait a few seconds and then start the real console application. Then, whenever you need the application to restart - you run the launcher and exit.

There might be other prettier methods, but you'd have to explain in more detail this part of your question: "I can't start two instances of the same app".

Paulius Maruška
A: 

you can put in the Main method a loop while a restart condition is true:

static void Main(string[] args)
{
    do
    {
        Main2(args);
        //Some cleaning may be...
    }while(someCondition)
}

private static void Main2(string[] args)
{
    ....
}
najmeddine
A: 

You could try creating a batch file to run your console application, and then a service to run the batch file if it's not currently running.

AKofC
A: 

I might be wrong, but a .NET console application that is single-instance seems like a design flaw. This sounds more like a Windows Service to me. Are you sure that a console application is the right thing, and that you cannot allow multiple instances?

Konrad Rudolph
Yes, I'm sure about it :) It's possible to have more than one same processes available, but they must be started from different folders.
Lukas Šalkauskas