|
-
Oct 17th, 2014, 08:14 AM
#1
Thread Starter
Member
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
-
Oct 17th, 2014, 08:36 AM
#2
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
-
Oct 17th, 2014, 08:39 AM
#3
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
-
Oct 17th, 2014, 01:15 PM
#4
Thread Starter
Member
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?
-
Oct 17th, 2014, 01:35 PM
#5
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
-
Oct 17th, 2014, 01:41 PM
#6
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
-
Oct 17th, 2014, 01:42 PM
#7
Thread Starter
Member
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!
-
Oct 17th, 2014, 02:17 PM
#8
Re: BackgroundWorker code won't copy files -- why?
walt4 techgnone told you that in post Three.
-
Oct 17th, 2014, 02:19 PM
#9
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!!!
-
Oct 18th, 2014, 05:44 AM
#10
Thread Starter
Member
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.
-
Oct 18th, 2014, 12:21 PM
#11
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.
-
Oct 18th, 2014, 01:03 PM
#12
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:
Public Class Form1 Private Sub Foo() Me.BeginInvoke(Sub() Me.Label1.Text = "" Me.Label2.Text = "" End Sub) End Sub End Class
-
Oct 18th, 2014, 01:20 PM
#13
Re: BackgroundWorker code won't copy files -- why?
 Originally Posted by ident
Kev is there a reason you choose not to invoke once with methodinvoker? curious thats all.
vb Code:
Public Class Form1 Private Sub Foo() Me.BeginInvoke(Sub() Me.Label1.Text = "" Me.Label2.Text = "" End Sub) End Sub 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
-
Oct 18th, 2014, 01:22 PM
#14
Thread Starter
Member
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.
-
Oct 18th, 2014, 03:26 PM
#15
Re: BackgroundWorker code won't copy files -- why?
 Originally Posted by walt4
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
-
Nov 26th, 2024, 07:31 AM
#16
Hyperactive Member
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.
-
Nov 26th, 2024, 09:04 AM
#17
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
-
Nov 26th, 2024, 09:27 AM
#18
Re: BackgroundWorker code won't copy files -- why?
 Originally Posted by techgnome
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|