C# - 进程没有终止

发布于 2024-08-17 18:22:38 字数 1300 浏览 1 评论 0原文

我正在恢复 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-08-24 18:22:38

您需要关闭 StandardInput 流:

stream.Close();
p.WaitForExit();

否则,程序将不会终止,因为它将等待更多输入。

You need to close the StandardInput stream:

stream.Close();
p.WaitForExit();

Otherwise, the program won't terminate because it will be expecting more input.

抽个烟儿 2024-08-24 18:22:38

代码在哪里?作为猜测,我想象输入流 (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 in Read 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文