C# - 进程没有终止

Jay*_*asi 1 c# process

我在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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的BackUp方法运行良好,但这种方法不起作用.(它们的变化很相似)

有什么建议?

wj3*_*j32 6

您需要关闭StandardInput流:

stream.Close();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)

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