Exit from parent process when child is running - c#

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.

Related

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.

Avoid locking service if process crashes / hangs

I am currently developing a windows service which implements fileSystemWatcher. Videos are uploaded into a folder at which point the filewatcher fires the created event as below to convert the video.
private void fileSystemWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
if (ConvertVideo(e.FullPath, e.Name))
{
WriteToEventLog(String.Format("Successfully converted video - {0}", e.FullPath), EventLogEntryType.Information);
}
}
Within ConvertVideo a new process is created but I have run into issues where the process crashes / hangs / disappears and it appears the main thread is then locked as its waiting for WaitForExit() which effectively crashes the service as no other videos can then be converted. How could I avoid locking the entire service if the process dies?
private bool ConvertVideo(string SourcePath, string Filename)
{
try
{
// Create new process
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "C:\Handbrake\HandBrakeCLI.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = GetArguments(SourcePath, Filename);
int? exitCode = null;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
exitCode = exeProcess.ExitCode;
}
}
catch(Exception ex)
{
return false;
}
}
NOTE: Code is shortened for this example
According to MSDN, Process.WaitForExit should return if your process crashes (emphasis added):
When an associated process exits (that is, when it is shut down by the
operation system through a normal or abnormal termination), the system
stores administrative information about the process and returns to the
component that had called WaitForExit().
It appears that your HandBrake process is just hanging and staying alive. The best solution would be to debug that process and figure out where it is crashing, but not closing down. Do you have access to the HandBrakeCLI.exe code?
If you don't have access to the HandBrake.exe code: you could use Process.WaitForExit(Int32) to set a timeout. If the timeout is reached, you may want to manually kill your process via the Process.Kill function, or all subsequent calls to Process.Start(ProcessStartInfo) will not work properly, since they will only return a new process if the process wasn'talready running:
A new Process component that is associated with the process resource,
or null if no process resource is started (for example, if an existing
process is reused).
1)
You should spawn your process and wait for it to terminate in a separate thread, in order to avoid blocking your main thread.
2)
You could use the WaitForExit method that takes the max time to wait for the process as a parameter. You'll then be able to avoid the case a thread of your program is blocked forever.

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?

System.Diagnostics.Process not Exiting in Code

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.

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