Re: [2008] Copying big files
Use a background worker to do the copy of the files.
Re: [2008] Copying big files
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.
Re: [2008] Copying big files
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
Re: [2008] Copying big files
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."
Re: [2008] Copying big files
Yep, just put your copying code into the DoWork function.
Re: [2008] Copying big files
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.""
Re: [2008] Copying big files
The error message is telling you exactly what you need to do. Modify the WorkerReportsProgress property.
Re: [2008] Copying big files
I realized and i fixed it, you replied too fast lol.. Thanks for the help.
1 Attachment(s)
Re: [2008] Copying big files
Next problem just came up. "Cross-thread operation not valid: Control 'txtInstallLog' accessed from a thread other than the thread it was created on."
and its reffering too the part of my code where i have:
vb Code:
Private Sub InstallLog(ByVal strLog As String)
txtInstallLog.Text = txtInstallLog.Text & strLog & vbCrLf
txtInstallLog.SelectionStart = txtInstallLog.Text.Length
End Sub
How do i allow Cross-thread/fix this?
Re: [2008] Copying big files
You will need to use a delegate if you want to access form controls from your background worker:
http://www.vbforums.com/showthread.p...light=delegate
Re: [2008] Copying big files
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.
Re: [2008] Copying big files
Here is a link that will help you immensely: Accessing Controls from Worker Threads
Re: [2008] Copying big files
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?
Re: [2008] Copying big files
Ive been doing that for every part that it throws the exception but now ive came to a stop:
vb Code:
Private Sub InstallLog(ByVal strLog As String)
If Me.txtInstallLog.InvokeRequired Then
Me.txtInstallLog.Invoke(New MethodInvoker(AddressOf InstallLog))
Else
Try
txtInstallLog.Text = txtInstallLog.Text & strLog & vbCrLf
txtInstallLog.SelectionStart = txtInstallLog.Text.Length
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
End Sub
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))"
Re: [2008] Copying big files
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
Re: [2008] Copying big files
I did. This is what i have (I for got to add the delegate part ot the post above):
vb Code:
Private Delegate Sub SetTextInvoker(ByVal strLog As String)
Private Sub InstallLog(ByVal strLog As String)
Try
txtInstallLog.Text = txtInstallLog.Text & strLog & vbCrLf
txtInstallLog.SelectionStart = txtInstallLog.Text.Length
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Me.txtInstallLog.Invoke(New MethodInvoker(AddressOf InstallLog))
What am i missing because i obviously am.
Re: [2008] Copying big files
I got it to stop showing the error but now i get "Parameter count mismatch."
vb Code:
Private Delegate Sub InstallLogInvoker(ByVal strLog As String)
Private Sub InstallLog(ByVal strLog As String)
Try
txtInstallLog.Text = txtInstallLog.Text & strLog & vbCrLf
txtInstallLog.SelectionStart = txtInstallLog.Text.Length
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Me.txtInstallLog.Invoke(New InstallLogInvoker(AddressOf InstallLog))
End Sub
Re: [2008] Copying big files
You are getting a parameter count mismatch, because you are not sending a parameter for strLog.
Code:
Me.txtInstallLog.Invoke(New InstallLogInvoker(AddressOf InstallLog),"Write this to the log.")
Re: [2008] Copying big files
So everytime its supposed say something i have to invoke it?
Re: [2008] Copying big files
I fixed that problem by doing this
vb Code:
Me.txtInstallLog.Invoke(New InstallLogInvoker(AddressOf InstallLog), "Currently installing, please wait.")
But how do i fix the one that shows status in my label? Its just a simple Status.Text = ("Installation Started.") and doesnt have its own sub.