Reputation: 31
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?
Upvotes: 1
Views: 340
Reputation: 8393
You need to close the StandardInput stream:
stream.Close();
p.WaitForExit();
Otherwise, the program won't terminate because it will be expecting more input.
Upvotes: 6
Reputation: 1062502
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).
Upvotes: 0