0

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";
        }
    }
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • Does this answer your question? [Batch script calling robocopy in Process won't terminate](https://stackoverflow.com/questions/44712364/batch-script-calling-robocopy-in-process-wont-terminate) – Selim Yildiz Jan 27 '20 at 16:38
  • 4
    Don't re-invent the wheel. Here's at least [one Robocopy GUI](https://sourceforge.net/projects/robomirror/?source=navbar); there are probably many more out there. This appears to just be a wrapper to the Robocopy binary, so the stopping/restarting behavior you mentioned should be preserved. – Patrick Tucci Jan 27 '20 at 16:43
  • You could use `proCopy.StartInfo.RedirectStandardInput = true;` and then write the byte 0x03 (end of text) to the input stream to simulate CTRL + C. May not be the most elegant solution but I don't see why it wouldn't work. – Jesse Jan 27 '20 at 16:43
  • @PatrickTucci If only my boss saw it your way. Sadly, this is the direction we're taking and I don't get a say. – Marlon_Brendo Jan 29 '20 at 11:31

0 Answers0