0

I want to exit the console window as soon as my files are written in folder at back end, but no matter whatever I try,

Environment.exit(0);
Environment.exit(1);
Environment.exit(-1);

Also since I am executing from the Main method, I am returning the value, but still my console window doesn't go off even after files are written to the destination folder.

Below is my code,

static void Main(string[] args)
{
    string execute = "";
    execute = data_info_pooling(args[0], args[1], args[2]);
    Environment.Exit(0);
}

Also I tried for using Application.exit();, but I am not able to get Application in drop-down box. I have explored almost all the possible helping links from Stack Overflow and searched for any help, but I have no idea where I am going wrong.

I am trying to run this console application by opening the command prompt and then executing the command as below

cd "Project Path\Debug"
"Project.exe" "First Parameter" " Second Parameter" "Third Parameter"`,

After files are written in the destination folder, the console window waits and after pressing Enter it just gives the path again to execute, but I want the window to exit as soon as the task of writing files is completed.

Second version of the code

I have deleted the for loop which is not necessary. Actually, I was wrong in application of my logic and apologize for my mistake. Some of the below comments helped me to realize my mistake. It's finally Environment.exit();

Which works. Also I would like to give a try to another answer of forming batch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Reshma
  • 864
  • 5
  • 20
  • 38
  • Have you tried `/C` argument switch as suggested in [this](http://stackoverflow.com/a/1469790/2974754) answer? – Yurii Jan 27 '14 at 07:21
  • 6
    You're already quitting the .NET process. It would be really odd to kill the command prompt it was running from, IMO. – Jon Skeet Jan 27 '14 at 07:22
  • You probably have a specific reason you want to close the window but in your example it seems silly. What if the user was doing other things and didn't want some executable to close their window? – crad Jan 27 '14 at 07:24
  • Actually..my genuine attempt is that I just want my console window to exit as soon as my results are stored in destination folder but the console window continues to stay on the screen...and need further processing like pressing any key to continue..I don't want this to happen. – Reshma Jan 27 '14 at 07:32
  • 2
    If you're managing threads properly, the application will be automatically exit as soon as the operations are finished. – N K Jan 27 '14 at 08:01
  • If you just commented the codes in the for loop, just for the sake of testing, is you console application exiting or not ? – sameh.q Jan 27 '14 at 08:03

3 Answers3

1

Environment.Exit and Application.Exit

Environment.Exit(0) is cleaner.

Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
1

You can also change the return value from void to int. Then you can simply return a ExitCode.

public static int Main(string[] args)
{
    string execute = "";
    for (int i = 0; i < args.Length; i++)
    {
        string argument = args[i];
        execute = data_info_pooling(args[0], args[1], args[2]);
    }

    return -1
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CodeTherapist
  • 2,776
  • 14
  • 24
0

As soon as your console application ends, the command window, created for it (assuming you run it from Windows Explorer) will be closed automatically.

If, however, you open a command window first, to specify parameters when running the EXE file, then Explorer will not close it (obviously).

One possible solution is to wrap the session into a batch file. Create project.bat with the following content:

Project.exe "First Parameter" " Second Parameter" "Third Parameter"
; some other jobs

Running that batch file (from Windows Explorer directly) will pass parameters to your application and the command window will be closed upon the end.

It may be what you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sinatr
  • 20,892
  • 15
  • 90
  • 319