Hello,
I made a small sample project in order to learn the use of a delegate to update the UI (older way / pre-Net4.0). I have no idea what might be wrong, since the UI is sometimes updating and sometimes not - but never in the expected way of counting upwards (visible), only the result is (mostly) shown.
As soon as I uncomment the "Thread.Sleep" line in ThreadClass, the UI freezes immediately after clicking the button.
Since I get such unpredictable behaviour, I'm already afraid I might have a completely unrelated problem... maybe with the VS setup. Would you be so kind as to have a look at the code and reassure me if it's correct or not? I don't comprehend why "Thread.Sleep(50)" would lead to a frozen UI.
Thank you very much,
Michael
EDIT: UI is frozen, but when I set a breakpoint I can see the counter increases. At the end of the loop, the UI is free again. So I must have twisted something...
What I have tried:
The code is:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
Dim foo As New ThreadClass
foo.LongLastingProcess(10000, AddressOf SetUIText)
End Sub
Friend Sub SetUIText(ByVal statusText As String, ByVal labelText As String)
If Me.InvokeRequired Then
Dim d As New UITestDelegate(AddressOf SetUIText)
Me.Invoke(d, New Object() {statusText, labelText})
Else
Me.ToolStripStatusLabel1.Text = statusText
Me.Label1.Text = labelText
End If
End Sub
in the form code, and another class
Friend Delegate Sub UITestDelegate(ByVal d As String, ByVal labelText As String)
Friend Class ThreadClass
Public Sub New()
End Sub
Friend Sub LongLastingProcess(ByVal number As Integer, ByVal UpdateMethod As UITestDelegate)
For i As Integer = 0 To number
Dim msg As String = String.Format("Loop {0}", i)
UpdateMethod.Invoke(msg, msg)
Next
End Sub
End Class