Results 1 to 18 of 18

Thread: BackgroundWorker code won't copy files -- why?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    63

    BackgroundWorker code won't copy files -- why?

    I was exploring how to use the BackgroundWorker with a very simple app to copy files from one folder to another. The code appears below.

    When I click the Copy Folders button (ButtonCopyFoldersClick), it should start the copying process. Instead, as soon as I click the button, the labelCopyingMessage displays the text "Copying completed".

    A quick check of the destination folder shows that contents of the source folder have not been copied to the destination folder.

    If I take the line My.Computer.FileSystem.CopyDirectory("E:\SourceFolder", "D:\DestinationFolder", True) out of the DoWork sub, and place it in the ButtonCopyFolderClick sub, the files do get copied; so the syntax of the copy code is OK.

    What am I missing here? There must be something I've overlooked, but what?

    Code:
    Imports System.Configuration
    Imports System.Diagnostics
    Imports System.Drawing
    Imports System.IO
    Imports System.IO.Ports
    Imports System.Management
    Imports System.Runtime.InteropServices
    
    Public Partial Class MainForm
    	Public Sub New()
    		Me.InitializeComponent()
    	End Sub
    	
    	Sub ButtonCopyFoldersClick(sender As Object, e As EventArgs)
    		BackgroundWorkerCopyFolders.RunWorkerAsync()
    	End Sub
    	
    	Sub BackgroundWorkerCopyFoldersDoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
    		labelCopyingMessage.Text = "Copying folder to destination"
    		My.Computer.FileSystem.CopyDirectory("E:\SourceFolder", "D:\DestinationFolder", True)
    	End Sub
    	
    	Sub BackgroundWorkerCopyFoldersRunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs)
    		labelCopyingMessage.Text = "Copying completed"
    	End Sub
    	
    End Class

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: BackgroundWorker code won't copy files -- why?

    Where is BackgroundWorkerCopyFolders being defined? I don't see anything that connects the sub BackgroundWorkerCopyFoldersDoWork to the BackgroundWorkerCopyFolders object. For that matter, I can't tell how BackgroundWorkerCopyFoldersRunWorkerCompleted is being hooked up either. Maybe it's my ignorance of the matter.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: BackgroundWorker code won't copy files -- why?

    Nope, dolt, you're not missing anything, the OP is... I don't see any Handles clause on any of the subs... so there's no events being handled. So I don't even see how ButtonCopyFoldersClick is event fired off... so that's problem 1. Problem 2 is that the DoWork and Completed subs are not hooked up as event handlers either. so even if ButtonCopyFoldersClick did run, the events in the BGW wouldn't fire off.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    63

    Re: BackgroundWorker code won't copy files -- why?

    Techgnome, the lack of handles was the issue. The code I posted was created in SharpDevelop, and apparently it manages the handles differently.

    I created the following code using VB2008, which works:

    Code:
    Imports System.Configuration
    Imports System.Diagnostics
    Imports System.Drawing
    Imports System.IO
    Imports System.IO.Ports
    Imports System.Management
    Imports System.Runtime.InteropServices
    Imports System.ComponentModel
    
    Public Class Form1
    
        Private Sub ButtonCopyFoldersClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCopyFoldersClick.Click
            labelCopyingMessage.Text = "Copying folder to destination"
            BackgroundWorkerCopyFolders.RunWorkerAsync()
        End Sub
    
        Private Sub BackgroundWorkerCopyFolders_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerCopyFolders.DoWork
            My.Computer.FileSystem.CopyDirectory("E:\SourceFolder", "D:\DestinationFolder", True)
        End Sub
    
        Private Sub BackgroundWorkerCopyFolders_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerCopyFolders.RunWorkerCompleted
            labelCopyingMessage.Text = "Copying completed"
        End Sub
    End Class
    The original SharpDevelop sub headers are formatted as follows:

    Sub BackgroundWorkerCopyFoldersDoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)

    and

    Sub BackgroundWorkerCopyFoldersRunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs)

    When I edit these to the following...

    Private Sub BackgroundWorkerCopyFoldersDoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerCopyFolders.DoWork

    and

    Private Sub BackgroundWorkerCopyFoldersRunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerCopyFolders.RunWorkerCompleted

    ...I get an error: The handles clause requires a WithEvents variable.

    As someone with limited programming experience, I'm not sure how to correct this. Can anyone offer some guidance on how to get the handles to work in SharpDevelop?

  5. #5
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: BackgroundWorker code won't copy files -- why?

    Never worked with SharpDevelop - always used Visual Studio.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: BackgroundWorker code won't copy files -- why?

    well somewhere there is a Dim BackgroundWorkerCopyFolders as .... it needs to have a WithEvents clause added to it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    63

    Re: BackgroundWorker code won't copy files -- why?

    I managed to find the answer to the handles problem on the SharpDevelop Forum (Here), and thought I'd post it here for the benefit of others who might be having a similar problem.

    In the Designer version of the form, the line declaring the background worker has to be changed.

    Original code:
    Public backgroundWorkerCopyFolders As System.ComponentModel.BackgroundWorker
    Amended code:
    Friend WithEvents backgroundWorkerCopyFolders As System.ComponentModel.BackgroundWorker

    This change enables the edited versions of the SharpDevelop headers (below) to work.

    Private Sub BackgroundWorkerCopyFoldersDoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerCopyFolders.DoWork

    Private Sub BackgroundWorkerCopyFoldersRunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerCopyFolders.RunWorkerCompleted

    Now the code works fine and the folders get copied!

    Thanks for the heads up on the headers!

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: BackgroundWorker code won't copy files -- why?

    walt4 techgnone told you that in post Three.
    My Github - 1d3nt

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: BackgroundWorker code won't copy files -- why?

    BTW what do you mean designer version of the form? I hope you don't mean in the forms designer!!!
    My Github - 1d3nt

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    63

    Re: BackgroundWorker code won't copy files -- why?

    I hope you don't mean in the forms designer!!!
    Ident, yes, I did mean the forms designer...

    In SharpDevelop if you drag a BackgroundWorker onto a form (while in design view) there seems to be no way to edit the properties to add a 'WithEvents' clause other than manually editing the Forms Designer code, which probably isn't a good idea.

    The only way to get around this seems to be to delete the BackgroundWorker that was dragged onto the form while in design view, and then declare the BackgroundWorker directly on the form along with the 'WithEvents' clause:

    Dim Friend WithEvents backgroundWorkerCopyFolders As New System.ComponentModel.BackgroundWorker
    Last edited by walt4; Oct 18th, 2014 at 06:51 AM.

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: BackgroundWorker code won't copy files -- why?

    Hello,

    If perhaps you were targeting Framework 4.5 (which I see your not) TAP which for many common operations is easier
    Code:
    Public Class Form1
        Private Async Sub cmdCopyFiles_Click(sender As Object, e As EventArgs) Handles cmdCopyFiles.Click
            For Each File As String In IO.Directory.EnumerateFiles(txtStartDirectory.Text)
    
                Using SourceStream As IO.FileStream = IO.File.Open(File, IO.FileMode.Open)
    
                    Using DestinationStream As IO.FileStream = IO.File.Create(txtEndDirectory.Text & File.Substring(File.LastIndexOf("\"c)))
                        Await SourceStream.CopyToAsync(DestinationStream)
                        Me.Label1.BeginInvoke(New Action(Sub() Label1.Text = "Copying: " & File))
                        Me.Label2.BeginInvoke(New Action(Sub() Label2.Text = "To: " & IO.Path.Combine(txtEndDirectory.Text, IO.Path.GetFileName(File))))
                        Await Task.Delay(100)
                    End Using
    
                End Using
    
            Next
        End Sub
    End Class
    In the code above we copy all files in a folder to another folder and the app stays responsive.

    Name:  TAP1.png
Views: 1840
Size:  20.8 KB

  12. #12
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: BackgroundWorker code won't copy files -- why?

    Kev is there a reason you choose not to invoke once with methodinvoker? curious thats all.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Foo()
    4.         Me.BeginInvoke(Sub()
    5.                            Me.Label1.Text = ""
    6.                            Me.Label2.Text = ""
    7.                        End Sub)
    8.     End Sub
    9.  
    10. End Class
    My Github - 1d3nt

  13. #13
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: BackgroundWorker code won't copy files -- why?

    Quote Originally Posted by ident View Post
    Kev is there a reason you choose not to invoke once with methodinvoker? curious thats all.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Foo()
    4.         Me.BeginInvoke(Sub()
    5.                            Me.Label1.Text = ""
    6.                            Me.Label2.Text = ""
    7.                        End Sub)
    8.     End Sub
    9.  
    10. End Class
    In the original example I did it has that and a tad more, somehow I did the dual invoke. This is the original were I use a function that returns the file count for copied files

    Code:
    Public Class Form1
        Private Async Sub cmdCopyFilesAsync_Click(sender As Object, e As EventArgs) Handles cmdCopyFilesAsync.Click
            Dim FileCount As Integer = Await CopyFilesAsync()
            MessageBox.Show("Copied " & FileCount.ToString & " files")
        End Sub
        Async Function CopyFilesAsync() As Task(Of Integer)
            Dim FileCount As Integer = 0
    
            For Each currentFile As String In IO.Directory.EnumerateFiles(txtStartDirectory.Text)
    
                Using sourceStream As IO.FileStream = IO.File.Open(currentFile, IO.FileMode.Open)
    
                    Using destinationStream As IO.FileStream = IO.File.Create(txtEndDirectory.Text & currentFile.Substring(currentFile.LastIndexOf("\"c)))
                        Await sourceStream.CopyToAsync(destinationStream)
                        Me.BeginInvoke(
                            Sub()
                                lblFrom.Text = "Copying: " & currentFile
                                lblToo.Text = IO.Path.Combine(txtEndDirectory.Text, IO.Path.GetFileName(currentFile))
                            End Sub)
    
                        ' Slow things down for demoing
                        Await Task.Delay(100)
    
                        FileCount += 1
                    End Using
    
                End Using
    
            Next
    
            Return FileCount
    
        End Function
    End Class

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    63

    Re: BackgroundWorker code won't copy files -- why?

    Kevin, that looks like an interesting approach... yes, I'm having to work with some machines that are only on .net 3.5.

  15. #15
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: BackgroundWorker code won't copy files -- why?

    Quote Originally Posted by walt4 View Post
    Kevin, that looks like an interesting approach... yes, I'm having to work with some machines that are only on .net 3.5.
    This should work, place a BackGroundWorker and Progressbar on the form, see the code below, replace the paths to match your requirements. Hope this helps

    Code:
    Imports System.Threading
    Public Class Form1
        Delegate Sub UpDateProcessBarDelegate(ByVal FileName As String, ByVal Counter As Integer)
        Private Waiting As Boolean = False
        Private StartDirectory As String = "C:\Data"
        Private EndDirectory As String = "C:\data\0Target"
        Private FileCount As Integer = 0
        Private Sub BackgroundWorker1_RunWorkerCompleted( _
            ByVal sender As Object, _
            ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
        Handles BackgroundWorker1.RunWorkerCompleted
    
            MessageBox.Show("Done")
    
        End Sub
        Private Sub BackgroundWorker1_DoWork( _
            ByVal sender As Object, _
            ByVal e As System.ComponentModel.DoWorkEventArgs) _
        Handles BackgroundWorker1.DoWork
    
            Dim Files As List(Of String) = _
                (From t In IO.Directory.GetFiles(StartDirectory) _
                 Select IO.Path.GetFileName(t)).ToList
    
            FileCount = Files.Count
    
            Dim UpdateListBoxDel As UpDateProcessBarDelegate = _
                New UpDateProcessBarDelegate(AddressOf UpdateProgressBar)
    
            Dim Counter As Integer = 1
    
            For Each file In Files
                Thread.Sleep(30)
    
                IO.File.Copy(IO.Path.Combine(StartDirectory, file), _
                             IO.Path.Combine(EndDirectory, file), True)
    
                If Not BackgroundWorker1.CancellationPending Then
                    Me.Invoke(UpdateListBoxDel, file, Counter)
                    Counter += 1
                Else
                    Exit For
                End If
            Next
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Me.BackgroundWorker1.RunWorkerAsync()
        End Sub
        Public Sub UpdateProgressBar(ByVal FileName As String, ByVal Counter As Integer)
            Me.ProgressBar1.Value = CInt((Counter / Me.FileCount) * 100)
        End Sub
    End Class

  16. #16
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: BackgroundWorker code won't copy files -- why?

    Wow, as an impartial contributor to this thread, I guarantee #15 is your/our answer since it isn't marked as resolved.

    You may alter the way you get/set paths in a dynamic way if you encounter 'path' does not exist exception like so:
    Code:
    Dim NewFolderName As String = "/BACKUP-" & Format(Now, "yyyy-MM-dd-HH-mm-ss")
    If Not System.IO.Directory.Exists(FolderBrowser.SelectedPath & NewFolderName) Then
        System.IO.Directory.CreateDirectory(FolderBrowser.SelectedPath & NewFolderName)
    End If
    Dim DestinitionDirectory = FolderBrowser.SelectedPath & NewFolderName
    BackUpThread.RunWorkerAsync()
    'New Source:		My.Application.Info.DirectoryPath
    'New Destination:	DestinitionDirectory
    I wonder what will happen to ProgressChanged event now on.

    Edit:
    Not sure what is the purpose of Thread.Sleep(30). I commented it and still works fine.

    Sorry for waking the dead, I use a different type of calendar in my country.
    Last edited by pourkascheff; Nov 27th, 2024 at 02:56 AM.

  17. #17
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: BackgroundWorker code won't copy files -- why?

    For crying outloud Porka... check the best by dates on threads before replying... nothing like replying to a 10 year-old htread to wake the dead....


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: BackgroundWorker code won't copy files -- why?

    Quote Originally Posted by techgnome View Post
    For crying outloud Porka... check the best by dates on threads before replying... nothing like replying to a 10 year-old htread to wake the dead....


    -tg
    ROFL! totally man! I was like "what? What thread is this? I haven't even visited this site in I don't know when."
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width