使用C#中的mysqldump降低性能 [英] Slow performance using mysqldump from C#

查看:114
本文介绍了使用C#中的mysqldump降低性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从C#控制台应用程序启动mysqldump:

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();

当我的数据库为空时,它工作得很好,但是当填充数据库时,它将永远花费...通过cmd启动命令仅需几秒钟.我可以看到它停在proc.WaitForExit()

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()

提前谢谢!

推荐答案

在StandardOutput.ReadToEnd()之前调用WaitForExit()可能会导致死锁,请先调用ReadToEnd(),这样就可以了.

Calling WaitForExit() before StandardOutput.ReadToEnd() can result in a deadlock condition, call ReadToEnd() first and you should be fine.

来自MSDN的详细信息, http://msdn .microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

Details from MSDN, http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

该代码示例通过以下方式避免了死锁情况 在p.WaitForExit之前调用p.StandardOutput.ReadToEnd.僵局 如果父进程在之前调用p.WaitForExit,则可能导致这种情况 p.StandardOutput.ReadToEnd,子进程将足够的文本写入 填充重定向的流.父进程将无限期等待 让子进程退出.子进程将等待 无限期地让父母从完整的StandardOutput中读取 流.

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.

有关此问题的帖子, ProcessStartInfo挂在"WaitForExit"上吗?为什么?

SO post regarding this issue, ProcessStartInfo hanging on "WaitForExit"? Why?

这篇关于使用C#中的mysqldump降低性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆