Jameel Moideen
Jameel Moideen

Reputation: 7941

Command prompt window can't close in .NET when calling Process.Start() method

I have created a Console application. In this application if any exception happens i tried to restart the exe by using Process.Start() method. The problem is that while executing this particular line of code a command prompt window will open and close. This process happens again and again. This is the code i have tried to restart the exe.

static void Main(string[] args)
{
    try
    {
        throw new ArgumentNullException();
    }
    catch (Exception ee)
    {
        ProcessStartInfo Info = new ProcessStartInfo();
        Info.Arguments = "/C \"" + Application.StartupPath + "\\AppRestart.exe" + "\"";
        Info.WindowStyle = ProcessWindowStyle.Normal;
        Info.CreateNoWindow = false;
        Info.FileName = "cmd.exe";
        Process.Start(Info);
        Environment.Exit(0);
    }
}

Upvotes: 0

Views: 863

Answers (4)

Steve
Steve

Reputation: 216353

I don't think you need a command window to run a console application.

ProcessStartInfo Info = new ProcessStartInfo();
Info.WindowStyle = ProcessWindowStyle.Normal;
Info.CreateNoWindow = false;
Info.FileName = "\"" + Application.StartupPath + "\\AppRestart.exe" + "\"";
Process.Start(Info);
Environment.Exit(0);

Actually, if you want to run something inside the command window you need to add the /C or /K flags otherwise the cmd.exe runs but doesn't execute anything.
In this case, if you really need to hide the command window you could set the

 Info.CreateNoWindow = true;
 Info.UseShellExecute = false;

but, it is not necessary to run your console application in a command window. It will create its own console to run into.

A side note, if the code of AppRestart is the one showed above, you enter an infinite loop. At first launch it throws unconditionally an exception, caught in the catch clause that restart the same application with the same unconditionally throw in the main code

static void Main(string[] args)
{
    if(conditionNotToThrow == false)
    {
        try
        {
            throw new ArgumentNullException();
        }
        catch(Exception ex)
        {

        }
     }
     else
     {
         // the code to resolve the problem for which this application has been made
     }
}

Upvotes: 1

bastos.sergio
bastos.sergio

Reputation: 6764

Try it like this:

static void Main(string[] args)
{
    try
    {
        throw new ArgumentNullException();
    }
    catch (Exception ee)
    {
        ProcessStartInfo Info = new ProcessStartInfo();
        Info.Arguments = "/C \"" + Application.StartupPath + "\\AppRestart.exe" + "\"";
        //Info.WindowStyle = ProcessWindowStyle.Normal;
        //Info.CreateNoWindow = false;

        // set window hidden
        Info.WindowStyle = ProcessWindowStyle.Hidden;
        Info.CreateNoWindow = true;
        // set window hidden

        Info.FileName = "cmd.exe";
        Process.Start(Info);
        Environment.Exit(0);
    }
}

Upvotes: 1

MartinStettner
MartinStettner

Reputation: 29174

This happens because you're in fact starting cmd.exe (the Windows command line processor) which is a console application.

Try

ProcessStartInfo Info = new ProcessStartInfo();
Info.FileName = Path.Combine(Application.StartupPath,"AppRestart.exe");
Info.WindowStyle = ProcessWindowStyle.Normal;
Info.CreateNoWindow = false;
Process.Start(Info);

instead

Upvotes: 2

tezzo
tezzo

Reputation: 11115

Try to restart directly your exe without using cmd.exe

Upvotes: 0

Related Questions