问题
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!
回答1:
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?
来源:https://stackoverflow.com/questions/9521644/slow-performance-using-mysqldump-from-c-sharp