I know that when you want to display a value from inside a thread , that you need to use delegates and invoke else there will be thrown illegal cross thread exception . In the following example I dont use delegates . Is it bad or not ? Should i avoid it and just use delegates to update the UI ?
Public Class Form1
Public Class Test
Public Property i As Integer = 0
Public Event ReturnValue(v As Integer)
Public Sub Start()
Dim T As Threading.Thread = New Threading.Thread(Sub()
While True
Threading.Thread.Sleep(100)
i += 1
RaiseEvent ReturnValue(i)
End While
End Sub)
T.Start()
End Sub
End Class
Public WithEvents TestInstance As New Test
Private Sub UpdateUI(ByVal v As Integer) Handles TestInstance.ReturnValue
Invoke(Sub()
Label1.Text = v.ToString
End Sub)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
TestInstance.Start()
End Sub
End Class