I was tasked with making a GUI for Robocopy so no technical people could use it, but now people want to be able to pause it and cancel it. Running it from the command line that's absolutely no problem, because you can terminate the batch job and it will automatically pick up from where it left off. From the program though I've hit a brick wall, I've had no luck with process.kill()
and similar, I'm guessing because I'm not able to pick up the original process.
Here's what I've got:
public void button1_Click(object sender, EventArgs e)
{
//RoboCopy();
MethodInvoker delagate = new MethodInvoker(RoboCopy);
delagate.BeginInvoke(null, null);
}
public void RoboCopy()
{
string OriginPath = txtOrigin.Text;
string DestinyPath = txtDestiny.Text;
DirectoryInfo w = new DirectoryInfo(OriginPath);
string DestinyFolder = DestinyPath + "\\" + w.Name;
using (Process proCopy = new Process())
{
if (Running == false)
{
proCopy.StartInfo.FileName = "cmd.exe";
proCopy.StartInfo.Arguments = "/c robocopy " + "\"" + OriginPath + "\" \"" + DestinyPath + "\\" + w.Name + "\" /E /V /COPY:DAT /DCOPY:T";
proCopy.StartInfo.RedirectStandardOutput = true;
proCopy.StartInfo.UseShellExecute = false;
proCopy.StartInfo.CreateNoWindow = true;
//Start Process
proCopy.Start();
proCopy.BeginOutputReadLine();
proCopy.WaitForExit();
result = proCopy.StandardOutput.ReadToEnd();
Running = false;
button1.Text = "Go";
}
else
{
proCopy.Dispose();
Running = false;
button1.Text = "Go";
}
}
}
Also tried:
private void button4_Click(object sender, EventArgs e)
{
using (Process proCopy = new Process())
{
if (Running == true)
{
proCopy.Kill();
Running = false;
button1.Text = "Go";
}
}
}