Hello friend Need help for the Following code .




Imports System.Threading

Public Class Form1
'txt1 = TextBox1
'txt2 = TextBox2
Dim counter1 As New Counter
Dim thread1 As New System.Threading.Thread(AddressOf counter1.count)
Private Sub BtnStartCounting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStartCounting.Click
txt2.Clear()
counter1.countTo = CInt(txt1.Text)
AddHandler counter1.FinishedCounting, AddressOf FinishedCountingEventHandler
thread1.Start()
End Sub

Public Sub FinishedCountingEventHandler(ByVal Count As Integer)
txt2.Text = Count.ToString
End Sub
End Class

Public Class Counter
Public countTo As Integer
Public Event FinishedCounting(ByVal Numberofmarches As Integer)
Public Sub count()
Dim loopIndex, Total As Integer
Total = 0
For loopIndex = 1 To countTo
Total += 1
Next loopIndex
RaiseEvent FinishedCounting(Total)
End Sub
End Class




While Executing the Programm. Getting Following Error.
-----
Cross-thread operation not valid: Control 'txt2' accessed from a thread other than the thread it was created on.


Help to to solve the Error.