I am stuck updating the progress indicator from another thread. I tried this in the simplest way, but then clearing the code makes me stuck.
My test code looks like all the examples on the Internet related to backgroundworker and BeginInvoke.
FormP is a Progressive Form. It works:
Public Class Form1 Private Delegate Sub delegate_ProgressUpdate(ByVal paramValue As Integer, ByVal paramMax As Integer) Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click ' Test 01: ' Show Progressbar via BGW ' All functions are within Form1 Dim bgw As New BackgroundWorker() AddHandler bgw.DoWork, AddressOf BGW_Sample01_DoWork FormP.Show(Me) bgw.RunWorkerAsync() End Sub Private Sub invokeMe_ProgressUpdate(ByVal paramValue As Integer, ByVal paramMax As Integer) FormP.ProgressBar1.Maximum = paramMax FormP.ProgressBar1.Value = paramValue FormP.ProgressBar1.Update() End Sub Private Sub BGW_Sample01_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) For i As Integer = 1 To 10 Threading.Thread.Sleep(500) ' Test delay Me.BeginInvoke(New delegate_ProgressUpdate(AddressOf invokeMe_ProgressUpdate), i, 10) Next MessageBox.Show("Fertig") End Sub
If I try to make the work more streamlined in FormP, it does not work.
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Dim bgw As New BackgroundWorker AddHandler bgw.DoWork, AddressOf BGW_Sample02_DoWork FormP.Show(Me) bgw.RunWorkerAsync() End Sub Private Sub BGW_Sample02_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) For i As Integer = 1 To 10 Threading.Thread.Sleep(500) FormP.SetProgress(i, 10) Next MessageBox.Show("Fertig") End Sub ' ########## FormP ################# Public Class FormP Private Delegate Sub delegate_ProgressUpdate(ByVal value As Integer, ByVal maximum As Integer) Public Sub SetProgress(ByVal paramValue As Integer, ByVal paramMaximum As Integer) If Me.InvokeRequired Then Me.Invoke(New delegate_ProgressUpdate(AddressOf Me.SetProgress), paramValue, paramMaximum) Else Me.ProgressBar1.Maximum = paramMaximum Me.ProgressBar1.Value = paramValue Me.ProgressBar1.Update() End If End Sub End Class
FormP does not freeze, but the user interface is not updated. In fact, Me.InvokeRequired is false, and I think where I begin to skip some important parts. I tried Form1.InvokeRequired here, but it is also false. My understanding: the calling thread here is the bgw thread, no matter what class this code calls this thread in ... It seems like it is not?
Thanks for any thoughts.