Reputation: 904
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.
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.
Upvotes: 0
Views: 12194
Reputation: 21969
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.
Upvotes: 0
Reputation: 2806
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
}
Upvotes: 1
Reputation: 28403
Environment.Exit
and Application.Exit
Environment.Exit(0)
is cleaner.
Upvotes: 1