2

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!

Kipters
  • 21
  • 1
  • WaitForExit() is not the problem.. I wonder what your SQL looks like.. if the db is empty.. then one must ask..what is it that you are trying to do..??? I would also suggest that you free the resources of the objects where you Create new Object.. also where is dump variable being declared..?? – MethodMan Mar 01 '12 at 18:19
  • for 'empty' I mean with about a dozen record across 8 tables, for 'populated' I mean about 10 thousand records in one table, 300 in another and 100 across the remaining tables... if I launch mysqldump via command line it takes a few seconds to dump a populated DB, via C#... well, it has been running for two hours and it hasn't finished yet... the dump streamwriter is declared on the second line after WaitForExit – Kipters Mar 01 '12 at 19:14

1 Answers1

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?

Community
  • 1
  • 1
Despertar
  • 21,627
  • 11
  • 81
  • 79