Visual basic 2019 robocopy cancel button

Brian 21 Reputation points

Hello Oracles, I'm a noob and attempting to convert some robocopy commands into a basic VB 2019 forms application. I've got the basics working, copying files and folders from one location to another, but I can't work out the code to put behind the 'Cancel' button. When I quit the app, robocopy keeps going. Please help the noob - thanks.

Button1 starts the robocopy process that cannot be terminated.

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim srce01 As String = "\MyServer\MyShare"
Dim dest01 As String = "MyLocalDrive"
Dim MyProcess01 As Process
MyProcess01 = New Process
With MyProcess01.StartInfo
.FileName = "C:\Windows\SysWOW64\robocopy.exe"
.Arguments = srce01 & " " & dest01 & " *.docx /MIR /s"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = False
.RedirectStandardOutput = False
.RedirectStandardError = True
End With
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
     **need code here to kill the robocopy process**
End Sub

End Class

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,851 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,600 questions
0 comments No comments
{count} votes

Accepted answer
  1. Adrian Bowles 86 Reputation points

    I believe it's a little more complicated as RoboCopy kicks of other process threads to copy files and killing the main process may eventually stop o copy or it may not.

    There's a thread about it on stack overflow and I seem to recall the issue on large file copies where killing robocopy still continued copying these processes until they completed. Rather than terminating them.

    So I think the key is to identify the child processes that are created, kill these and then kill the originating process.

    Something like

        Sub KillProcessAndChildren(PID As Integer)
            Dim searcher As New ManagementObjectSearcher(String.Format("Select * From Win32_Process Where ParentProcessID={0}", PID))
            Dim moc As ManagementObjectCollection = searcher.Get()
                For Each mo In moc
                    Catch ex As Exception
                        '//Don't worry as simply trying to kill all children
                    End Try
                Dim proc = Process.GetProcessById(PID)
            Catch ex As ArgumentException
                ' // Process already exited.
            End Try
        End Sub

    For simple processes like Notepad - you can simply kill the process itself like KarenPayneOregon suggested

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,196 Reputation points

    See if this would work for you, here I'm opening a text file.

    Imports System.IO
    Public Class Form1
        Public Process As New Process()
        Public Sub ExecuteCommand(pCommandLineFile As String, Optional pArguments As String = "")
            Process.StartInfo.FileName = pCommandLineFile
            If Not String.IsNullOrWhiteSpace(pArguments) Then
                Process.StartInfo.Arguments = pArguments
            End If
            Process.StartInfo.CreateNoWindow = True
            Process.StartInfo.UseShellExecute = False
        End Sub
        Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
            Dim fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt")
            ExecuteCommand("Notepad.exe", fileName)
        End Sub
        Private Sub KillButton_Click(sender As Object, e As EventArgs) Handles KillButton.Click
        End Sub
    End Class