Data Copy Operation and Process excecution in VB

~OSD~ 2,196 Reputation points
2020-12-16T12:12:29.163+00:00

Hi,

I would like to copy a folder (with all it's contents - files /folders /hidden files folders /empty directories etc.)
And need to use this newly copied data to start a process, ... but if the copying operation is not finished already, the next process will give error.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Computer.FileSystem.CopyDirectory("C:\CopyFileDemo", "D:\CopyFileDemo")
Process.Start("D:\CopyFileDemo\MyData\MyApp.exe")
End Sub

In other words, I am looking for a way to copy directory first
My.Computer.FileSystem.CopyDirectory("C:\CopyFileDemo", "D:\CopyFileDemo")

and once this task is finished, proceed with the next task:
Process.Start("D:\CopyFileDemo\MyData\MyApp.exe")

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,366 Reputation points
    2020-12-17T02:13:54.98+00:00

    Hi @~OSD~ ,
    You can also consider using Task.ContinueWith method which creates a continuation that executes asynchronously when the target Task completes.
    Here's the code you can refer to.

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            Task.Factory.StartNew(AddressOf CopyDirectory).ContinueWith(Sub(task)  
                                                                            Process.Start("D:\CopyFileDemo\MyData\MyApp.exe")  
                                                                        End Sub)  
        End Sub  
        Private Sub CopyDirectory()  
            My.Computer.FileSystem.CopyDirectory("C:\CopyFileDemo", "D:\CopyFileDemo")  
        End Sub  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Anonymous
    2020-12-16T14:14:09.813+00:00

    Hi
    Here is the basic simplified layout for a BackGroundWorker showing how to use the RunAsync, Report Progress and Completed events. NOTE: you can NOT use the Form controls directly from the running BGW (see the Invoke I used for the TextBox).

    This is JUST a simple example and does nothing.
    The example needs a multiline TextBox1 and a Button1 on Form1.

    ' EXAMPLE for BGW
    ' Form1 with multiline
    ' TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Imports System.ComponentModel
    Public Class Form1
      Dim WithEvents bgw As New BackgroundWorker
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bgw.WorkerReportsProgress = True
        bgw.WorkerSupportsCancellation = True
        Button1.Text = "START"
        TextBox1.AppendText("Waiting ...." & vbCrLf & vbCrLf)
    
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' start the BGW if
        ' not already running
        If Not bgw.IsBusy Then
          TextBox1.AppendText("BGW is starting ...." & vbCrLf)
          Button1.Text = "STOP"
          bgw.RunWorkerAsync()
        Else
          bgw.CancelAsync()
        End If
      End Sub
      Private Sub BGW_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork
        ' BGW running
        ' do all the File operations
    
        Invoke(Sub() TextBox1.AppendText("BGW is running ...." & vbCrLf))
    
        Dim c As Integer = 0
        Do
          c += 1
          Threading.Thread.Sleep(1000)
          bgw.ReportProgress(c)
        Loop While Not bgw.CancellationPending
    
      End Sub
      Private Sub BGW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        ' BGW completed
        ' do Process Start
        TextBox1.AppendText("BGW Completed ...." & vbCrLf & vbCrLf)
        Button1.Text = "START"
      End Sub
      Private Sub BGW_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bgw.ProgressChanged
        ' can be used to update User
        ' on progress etc
        TextBox1.AppendText("BGW Progress ...." & e.ProgressPercentage.ToString & vbCrLf)
    
      End Sub
    End Class
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.