System.Diagnostics.Process not Exiting in Code - c#

I have the following code which works well on another server. The problem is that the process never seems to make it to an Exited state. The exe being called creates a file as the last step and this file does get created but my code never seems to know that the process has completed. Also the exe being called runs in much less than 10 seconds when ran manually. My code looks like this:
System.Diagnostics.Process proc = new System.Diagnostics.Process() proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = exeConf.CMD;
proc.StartInfo.Arguments = argString;
proc.Start();
proc.WaitForExit(10000);
if(proc.HasExited)
msgLine = proc.StandardError.ReadToEnd();

See this MSDN article.
A deadlock condition can result if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.

It seems as if Process.StandardOutput.ReadToEnd() has to be called immediately after Process.Start() else it could create a deadlock.

Related

How to stop ffmpeg process after it has finished processing in C#?

I am trying to stop the ffmpeg process once it has finished doing what I want it do, but I am not able to find a way.
Here is what I have done.
//Process for running ffmpeg
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = ffmpegfile;
process.StartInfo.Arguments = commandtorun;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit();
process.Close();
The problem is ffmpeg does not tell the process to stop after it has finished executing, so I cant use WaitForExit() call.
What i tried doing is
commandtorun = commandtorun+ " && exit";
to force ffmpeg to close after it finishes executing. Now this works when I try in cmd.
But when I do the same thing in C#, ffmpeg closes down as soon as the command is executed.
Is there any way to force ffmpeg or the process to close after the processing is done ?
Try
process.StartInfo.UseShellExecute = true;
This will force the process to run in shell. I don't know why it did not work using standard execution but sometimes processes behave differently in shell execute.

Program freezes after clicking button?

When I click a button, the program freezes.
I am trying to reach file.bat in h folder of the root directory.
this is my code for click event:
private void button1_Click_1(object sender, EventArgs e)
{
{
string pathName = textBox.Text;
pathName = Path.GetFileName(pathName);
string dir = System.Windows.Forms.Application.StartupPath;
string dirEnd = dir + "\\h\\";
Process proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "\"" + dirEnd + "file.bat" + "\"";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
MessageBox.Show("Program has been started!");
}
If I remove proc.WaitForExit(); nothing will happen but the program wont freeze.
But if I remove proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; the CMD will start but the argument wont be passed to it.
Process.WaitForExit();
From the docs:
Instructs the Process component to wait indefinitely for the associated process to exit.
This means that the Process.WaitForExit(); method blocks until the process finishes. If the process runs for a long time your application will just wait, it's not actually frozen, it's just doing what it's told.
If you don't actually want to wait for it to finish and show your message instead just remove the statement like this:
proc.Start();
MessageBox.Show("Program has been started!");
Edit
There's something wrong with your argument. While debuging, remove proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; and proc.WaitForExit(); so you can see what happens.
Build your argument string in a seperate variable and inspect it to make sure it's correct.
If you want to run a command with cmd.exe, you need to pass the /C argument. For example: cmd.exe ping won't work, you must use cmd.exe /C ping. In your case the argument should probably be something like: /C path/to/file.bat.
This line will make your program hang:
proc.WaitForExit();
You will never get to the messagebox showing because the application is waiting for your process to exit. Just remove proc.WaitForExit() and your message will show while the process is still running in the background. However, if you do this you have to make sure everything is handled properly (i.e. process dies when your application closes)
According to MSDN, WaitForExit waits for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited. 'The current thread' being the thread that you launched it from; in this case, your main program, causing your hang.

Slow performance using mysqldump from C#

I'm trying to launch mysqldump from my C# console application using this code:
ProcessStartInfo procInfo = new ProcessStartInfo("mysqldump", "avisdb -uroot -p" + cs.Password);
procInfo.CreateNoWindow = true;
procInfo.RedirectStandardOutput = true;
procInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = procInfo;
proc.Exited += new EventHandler(proc_Exited);
proc.Start();
proc.WaitForExit();
File.Delete("dump.sql");
StreamWriter dump = File.AppendText("dump.sql");
dump.Write(proc.StandardOutput.ReadToEnd());
dump.Flush();
dump.Close();
it works great when my db is empty, but it takes forever when DB is populated... Launching the command via cmd it takes just a few seconds. I can see it stalls on proc.WaitForExit()
Thanks in advance!
Calling WaitForExit() before StandardOutput.ReadToEnd() can result in a deadlock condition, call ReadToEnd() first and you should be fine.
Details from MSDN, http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
The code example avoids a deadlock condition by
calling p.StandardOutput.ReadToEnd before p.WaitForExit. A deadlock
condition can result if the parent process calls p.WaitForExit before
p.StandardOutput.ReadToEnd and the child process writes enough text to
fill the redirected stream. The parent process would wait indefinitely
for the child process to exit. The child process would wait
indefinitely for the parent to read from the full StandardOutput
stream.
SO post regarding this issue,
ProcessStartInfo hanging on "WaitForExit"? Why?

Exit from parent process when child is running

i have a WinForms application. And here is code from Form_Load method.
Process proc = new Process();
proc.StartInfo.FileName = #"C:\program files\MyProgram\start.exe";
proc.StartInfo.Arguments = Application.ExecutablePath;
proc.Start();
Application.Exit();
Why is my parent process doesnt close program when i call Application.Exit? How to detect problem?
Once you start a new process, its lifetime is independent of your current application’s. If you want an execution artefact that is directly tied to the lifetime of the current application, use threads.
If you’re sure you want to create a child process and then terminate it, you need to kill it:
Process proc = new Process();
proc.StartInfo.FileName = #"C:\program files\MyProgram\start.exe";
proc.StartInfo.Arguments = Application.ExecutablePath;
proc.Start();
// When terminating:
proc.Kill();
proc.WaitForExit();
Application.Exit();
However, I wouldn’t recommend the above for most scenarios, since it can lead to data corruption if the child process is killed whilst performing a critical operation (such as saving to file).
In order to exit a Windows Forms application, you should close the main form. Look at the code in Program.Main() and you'll see that what keeps the main thread alive is the main form. Close it and the application will exit.

Getting a Process to terminate

I have a process object setup like the following:
Process p = new Process();
p.StartInfo.FileName = command;
p.StartInfo.UseShellExecute = true;
p.StartInfo.Arguments = String.Format(
commandArguments,
destinationLocation,
sourceLocation,
sourceDirName,
(string.IsNullOrEmpty(revisionNotes.Text)) ? "" : revisionNotes.Text);
(where undefined values are supplied externally to this code and are valid). The process in question launches and properly executes with p.Start(); but i need to catch it on termination. The console window flashes up briefly and goes away which would seem to indicate that the process is done, but none of the relevant events are fired (OutputDataRecieved, Exited, etc) and it's like the process never ends. (I'm trying to execute a lua script with some parameters if that's relevant). Can someone help me get this process to stop correctly?
WaitForExit
Have you set the EnableRaisingEvents property of the process to True? You won't catch the Exited event without it.

Categories