I can't get my backgroundworker to update my form.
This is just a test, trying to learn HOW to do it before I use it for anything serious.
I can run a procedure from the main thread, it works and updates the form, but locks up all the other controls while running.
I can run an identical procedure from a backgroundworker. I can step through it and see it working, but it doesn't update the form.
The only difference between the two is the second uses reportprogress.
=========
My form has 2 command buttons
- name= cmd_Start, text = "Start"
- name = cmd_thread, text = "Asynch"
It has three labels
- lbl_HH, lbl_MM, lbl_SS
It has one backgroundworker
- name = bgw_1, workerreportsprogress=true, workersupportscancellation=true
Code for the form
Module code
Class code
This is just a test, trying to learn HOW to do it before I use it for anything serious.
I can run a procedure from the main thread, it works and updates the form, but locks up all the other controls while running.
I can run an identical procedure from a backgroundworker. I can step through it and see it working, but it doesn't update the form.
The only difference between the two is the second uses reportprogress.
=========
My form has 2 command buttons
- name= cmd_Start, text = "Start"
- name = cmd_thread, text = "Asynch"
It has three labels
- lbl_HH, lbl_MM, lbl_SS
It has one backgroundworker
- name = bgw_1, workerreportsprogress=true, workersupportscancellation=true
Code for the form
VB.NET:
Public Class Form1
Private Sub cmd_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_start.Click
Dim my_clock As New Class_Clock
my_clock.run_Clock()
End Sub
Private Sub cmd_thread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_thread.Click
start_thread()
End Sub
Private Sub BackgroundWorker1_DoWork(
ByVal sender As System.Object,
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw_1.DoWork
Dim my_clock As Class_Clock = CType(e.Argument, Class_Clock)
my_clock.run_Clock_asynch()
End Sub
Private Sub BackgroundWorker1_ProgressChanged(
ByVal sender As System.Object,
ByVal e As System.ComponentModel.ProgressChangedEventArgs
) Handles bgw_1.ProgressChanged
Dim state As Class_Clock.CurrentState = CType(e.UserState, Class_Clock.CurrentState)
lbl_HH.Text = state.Hours
lbl_HH.Refresh()
lbl_MM.Text = state.Minutes
lbl_MM.Refresh()
lbl_SS.Text = state.Seconds
lbl_SS.Refresh()
End Sub
End Class
Module code
VB.NET:
Module Module_1
Sub start_thread()
Dim my_clock As New Class_Clock
Form1.bgw_1.RunWorkerAsync(my_clock)
End Sub
End Module
Class code
VB.NET:
Public Class Class_Clock
Public Class CurrentState
Public Hours As Integer
Public Minutes As Integer
Public Seconds As Integer
End Class
Public Sub run_Clock()
Dim s As New CurrentState
For s.Hours = 0 To 24
Form1.lbl_HH.Text = s.Hours
Form1.lbl_HH.Refresh()
For s.Minutes = 0 To 59
Form1.lbl_MM.Text = s.Minutes
Form1.lbl_MM.Refresh()
For s.Seconds = 0 To 59
Form1.lbl_SS.Text = s.Seconds
Form1.lbl_SS.Refresh()
Threading.Thread.Sleep(10)
Next 'seconds
Next 'minutes
Next 'hours
End Sub
Public Sub run_Clock_asynch()
Dim s As New CurrentState
For s.Hours = 0 To 24
Form1.lbl_HH.Text = s.Hours
Form1.lbl_HH.Refresh()
For s.Minutes = 0 To 59
Form1.lbl_MM.Text = s.Minutes
Form1.lbl_MM.Refresh()
For s.Seconds = 0 To 59
Form1.lbl_SS.Text = s.Seconds
Form1.lbl_SS.Refresh()
Threading.Thread.Sleep(10)
Form1.bgw_1.ReportProgress(0, s)
Next 'seconds
Next 'minutes
Next 'hours
End Sub 'run_Clock_asynch
End Class