Hi,
Sorry for dragging this post back up but its the best example I found.
I'm also a bedroom hobbyist and therefore don't have any real expirence in programming so forgive me for if this is a dumb question.
I have a background worker within my form and a progress bar which works fine whilse the work being carried out is within the same form. If I move the work to a Module the work still gets carried out but my progress bar doesn't update anymore. My understanding is this is because the work and progress bar are not within the same ui thread but I dont understand how to fix this issue.
Here's a quick example of a test I setup...
My Main Form
Code:
Public Class Form_Test
Delegate Sub SetLabelText_Delegate(ByVal Label As Label, ByVal [text] As String)
Public Sub SetLabelText_ThreadSafe(ByVal Label As Label, ByVal [text] As String)
If Label.InvokeRequired Then
Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
Me.Invoke(MyDelegate, New Object() {Label, [text]})
Else
Label.Text = [text]
End If
End Sub
Public Sub test()
Dim i As Integer = 0
Do While i < 20
i = i + 1
Me.BackgroundWorker1.ReportProgress(CInt((i / 20) * 100))
Me.SetLabelText_ThreadSafe(Label1, "Same Form - " & FormatPercent(i / 20, 0))
System.Threading.Thread.Sleep(100)
Debug.Print("Running Test 2 From Within The Same Form: " & i)
Loop
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_DoWork_1(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
test()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker2_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Public Sub BackgroundWorker2_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
test_2()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.BackgroundWorker2.RunWorkerAsync(2)
End Sub
End Class
My Module Code
Code:
Module Module_Test
Public Sub test_2()
Dim i As Integer = 0
Do While i < 20
i = i + 1
Form_Test.BackgroundWorker1.ReportProgress(CInt((i / 20) * 100))
Form_Test.SetLabelText_ThreadSafe(Form_Test.Label1, "Module - " & FormatPercent(i / 20, 0))
System.Threading.Thread.Sleep(100)
Debug.Print("Running Test 2 From Within A Module: " & i)
Loop
End Sub
End Module
Anyone got any suggestions?
Many Thanks
Ben