C# - 进程没有终止
我正在恢复 MySql 中的备份。但 mysql exe 并未终止。 这是我的代码 -
public override bool FullRestore(Stream fileStream)
{
try
{
ProcessStartInfo proc = new ProcessStartInfo();
string cmd = string.Format("--database {0} --user={1} --password={2}", config.GetDbName(), config.GetUserName(), config.GetPassword());
proc.FileName = "mysql";
proc.RedirectStandardInput = true;
proc.RedirectStandardOutput = false;
proc.Arguments = cmd;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
Stream stream = p.StandardInput.BaseStream;
Stream file = Utility.ZipNEncrypt.Unzip(fileStream, "XXXXXX");
byte[] bytes = new byte[1024];
for (int count = 0; (count = file.Read(bytes, 0, 1024)) > 0; )
{
stream.Write(bytes, 0, count);
}
stream.Flush();
p.WaitForExit();
file.Close();
return true;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
return false;
}
}
我的备份方法运行良好,但此方法不起作用。(它们非常相似)
有什么建议吗?
I am restoring backup in MySql. But the mysql exe is not terminating.
This is my code -
public override bool FullRestore(Stream fileStream)
{
try
{
ProcessStartInfo proc = new ProcessStartInfo();
string cmd = string.Format("--database {0} --user={1} --password={2}", config.GetDbName(), config.GetUserName(), config.GetPassword());
proc.FileName = "mysql";
proc.RedirectStandardInput = true;
proc.RedirectStandardOutput = false;
proc.Arguments = cmd;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
Stream stream = p.StandardInput.BaseStream;
Stream file = Utility.ZipNEncrypt.Unzip(fileStream, "XXXXXX");
byte[] bytes = new byte[1024];
for (int count = 0; (count = file.Read(bytes, 0, 1024)) > 0; )
{
stream.Write(bytes, 0, count);
}
stream.Flush();
p.WaitForExit();
file.Close();
return true;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
return false;
}
}
My BackUp method working well, but this method is not working.(They are vary much similar)
Any Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要关闭 StandardInput 流:
否则,程序将不会终止,因为它将等待更多输入。
You need to close the StandardInput stream:
Otherwise, the program won't terminate because it will be expecting more input.
代码在哪里?作为猜测,我想象输入流 (
fileStream
) 没有 EOF(它可能是来自另一个进程的传入管道?或网络流?),因此您正在等待读取
当前数据的末尾。尝试关闭传入流?(它可能被称为
fileStream
,但我并没有假设它实际上是一个文件/FileStream
...我已经之前就被这样的假设所困扰;-p)您可能还需要考虑适当地引入
using
(也许是file
)。Where is the code sitting? As a guess, I would imagine that the input stream (
fileStream
) doesn't have an EOF (is it perhaps an incoming pipe from another process? or a network stream?) and therefore you are waiting inRead
at the end of the current data. Try closing the incoming stream?(it might be called
fileStream
, but I'm not making the assumption that it is actually a file /FileStream
... I've been bitten by such assumptions before ;-p)You also might want to think about introducing
using
as appropriate (file
, perhaps).