On my dual core system I thought the UI would get updated sometimes when executing this code:
but it does not until the end. I didn't expect it to be pretty. Using the AutoResetEvent works, as would Invoke.Code:Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim t As New Threading.Thread(AddressOf foo) t.IsBackground = True t.Start() End Sub Private Sub foo() For x As Integer = 1 To 50000 updUI(x) 'Threading.Thread.Sleep(0) Next End Sub 'Dim updUIare As New Threading.AutoResetEvent(True) Delegate Sub updUIdel(i As Integer) Private Sub updUI(i As Integer) If Me.InvokeRequired Then 'updUIare.WaitOne() Dim d As New updUIdel(AddressOf updUI) Me.BeginInvoke(d, i) Else Label1.Text = i.ToString("n0") ProgressBar1.Value = i Mod 100 'updUIare.Set() End If End Sub
This is a simple reproduction of some code I was testing for a worst case situation.




Reply With Quote