I have a code that copies a folder of files to different directory and sorts them and they are about 700mb but the problem im having is that when it runs on form load the form freezes and doesnt refresh till its done so my question is what can i use that can handle this better?
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
will i still be able to show the status of how things are going? Also how do i copy using background worker? Ive been searching google and cannot find anything to help me out.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
The background worker is a component you can add to a form from the designer. The background worker also has a ProgressChanged event, which you can fire and then in that event handler you can update a progress bar on the main form, for example:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i As Integer = 0 To 20
System.Threading.Thread.Sleep(500)
BackgroundWorker1.ReportProgress(i * 5)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Ok i understand that much but do i use the same code as i would in a regular form to copy?
Edit also i tried debugging as is and i get this error:
"This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress."
Last edited by youngbucks; Jul 17th, 2008 at 07:44 PM.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
Ok thanks but one more thing, i keep getting this error:
""This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress.""
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
I dont understand 100% on what to do here as its throwing an exception to the labels and textbox i have on the form and since its in Backgroundworker1 im confused at to what goes in Addressof or what to put as string(I know what must be done i just dont know how to do it). Please bare with me as im totally new to using background worker.
Last edited by youngbucks; Jul 17th, 2008 at 08:29 PM.
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
I had a look at that also but what would i put here "(AddressOf ResetTextBoxText)" . My status(label) is supposed to change the text multiple times throughout and display something else so how do i end up setting that up?
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
The error i get is "Method 'Private Sub InstallLog(strLog As String)' does not have a signature compatible with delegate 'Delegate Sub MethodInvoker()'.
" and it give me the error in this line "Me.txtInstallLog.Invoke(New MethodInvoker(AddressOf InstallLog))"
"Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz
Did you read the article we posted? It goes over the exact scenario if you have to pass parameters to the fucntion call. It's the second post. Basically you need to create your own delegate type to handle the scenario:
Code:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i As Integer = 0 To 20
System.Threading.Thread.Sleep(500)
BackgroundWorker1.ReportProgress(i * 5)
TextBox1.Invoke(New UpdateTextInvoker(AddressOf UpdateText), i * 5 & " % Complete")
Next
End Sub
Private Delegate Sub UpdateTextInvoker(ByVal Value As String)
Private Sub UpdateText(ByVal Value As String)
TextBox1.Text &= Value & Environment.NewLine
End Sub