1

I write this code bellow to run an .exe file in my program and its work just fine except that the process never end so the program don't proceed after the proc.WaitForExit(); line. I tried to read the StandartInput and nothing change. What can be the reason?

My code:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.WorkingDirectory = @"D:\";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
proc.StandardInput.WriteLine("cd " + directory);
proc.StandardInput.WriteLine(exeFile + arguments);
proc.WaitForExit();`

Note: if i run the exe file through cmd in windows its work great.

Thank you.

Nino
  • 6,931
  • 2
  • 27
  • 42
  • 2
    try using `WaitForExit(int milliseconds)` method overload, ie `prod.WaitForExit(1000);` – Nino Mar 07 '17 at 10:22
  • also, check [this question](http://stackoverflow.com/questions/139593/processstartinfo-hanging-on-waitforexit-why) – Nino Mar 07 '17 at 10:22
  • I prefer not use `WaitForExit(int miliseconds)` because I cant know how much time this process will take and this can cause some problems/slow work. –  Mar 07 '17 at 10:26
  • you basically open `cmd` and do not close it, by using `proc.WaitForExit()` you just wait untill user closes cmd. Note that `proc.StartInfo.CreateNoWindow = true;` just not open the console but still run a cmd process, so you need to explicitly stop the process. – Vladimir Mar 07 '17 at 10:48
  • But how can I know when to close the window? I try to use `StandardOutput.ReadToEnd()` but this didnt help - maybe bacause the exe file dont write any output/ –  Mar 07 '17 at 10:57
  • So you basically start exe from the `cmd` ? Consider to start that exe file directly in the proc process without using the console, it will give you full controll over the running process, and you will be able to see when process terminates. – Vladimir Mar 07 '17 at 11:03
  • I meant that if I run the exe file throw windows then its work fine but I need that it will run trow my code. –  Mar 07 '17 at 11:09

1 Answers1

1

Let`s look at you code to see a reason why process is running :

1.) You create a Process

 System.Diagnostics.Process proc = new System.Diagnostics.Process();

2.) You initialize it:

 proc.StartInfo.FileName = "cmd.exe";
 proc.StartInfo.WorkingDirectory = @"D:\";
 proc.StartInfo.UseShellExecute = false;
 proc.StartInfo.RedirectStandardOutput = true;
 proc.StartInfo.RedirectStandardInput = true;

That part make sure that user do not see the console:

 proc.StartInfo.CreateNoWindow = true;  

Starting the process:

 proc.Start();

Use the cmd to go to the directory and execute some exe file:

 proc.StandardInput.WriteLine("cd " + directory);
 proc.StandardInput.WriteLine(exeFile + arguments);

Now waiting until process will be stoped

 proc.WaitForExit();

But it won`t because you do not close that process - cmd even without visible console is still running - that is the problem.

so it`s better to use:

 proc.Close();

after all necessary operation has been completed in cmd.

Other problem arises - that you should know when executable is stopped, cmd won`t help you there.

Might consider to use process without cmd:

 proc.StartInfo.FileName = "exeFile";
 proc.StartInfo.Arguments = "arguments";
 proc.StartInfo.WorkingDirectory = "directory";
 proc.StartInfo.UseShellExecute = false;
 proc.StartInfo.RedirectStandardOutput = true;
 proc.StartInfo.RedirectStandardInput = true;

Now you would have full control over the running process, and know when it terminates.

Vladimir
  • 1,380
  • 2
  • 19
  • 23