Ather.

i am creating a thread the thread Starts and Suspend successfully first time but when i again start the error occur

Thread is running or terminated; it cannot restart.

so how can i restart a thread again and again....for example start then stop then start and stop ..........start and stop.

the second problem is i want to see the counting in the textbox but when i assign the " i " value to the textbox.text the following error occur

Cross-thread operation not valid: Control 'txtThread1' accessed from a thread other than the thread it was created on.

here is my coding

Public t1 As New Threading.Thread(AddressOf Increment1)

Private Sub btnT1Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT1Start.Click

If Me.btnT1Start.Text.ToLower = "start" Then
Me.btnT1Start.Text = "end"
Try
If t1.ThreadState = Threading.ThreadState.Unstarted Then
t1.Start()
Me.lblMessage.Text = "Unstarted Start"
ElseIf t1.ThreadState = Threading.ThreadState.Suspended Then
t1.Resume()
Me.lblMessage.Text = "Suspended Resume"
'ElseIf t1.ThreadState = Threading.ThreadState.Stopped Then
' t1.Resume()
' t1.Abort()
' Me.lblMessage.Text = "Stopped Start"
ElseIf t1.ThreadState = Threading.ThreadState.Aborted Then
t1.Resume()
Me.lblMessage.Text = "Aborted resume"
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
ElseIf Me.btnT1Start.Text.ToLower = "end" Then
Me.btnT1Start.Text = "start"
Try
If t1.ThreadState = Threading.ThreadState.Running Then
t1.Suspend()
Me.lblMessage.Text = "Suspand"
Me.txtThread1.Text = i
ElseIf t1.ThreadState = Threading.ThreadState.WaitSleepJoin Then
t1.Suspend()
Me.lblMessage.Text = "Wait Sleep Join Suspand"
Me.txtThread1.Text = i
ElseIf t1.ThreadState = Threading.ThreadState.Suspended Then
t1.Suspend()
Me.lblMessage.Text = "Suspand"
Me.txtThread1.Text = i
ElseIf t1.ThreadState = Threading.ThreadState.Stopped Then
Me.lblMessage.Text = "Stopped"
Me.txtThread1.Text = i
End If

Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End If

End Sub

Public Sub Increment1()

For i = 1 To 1000
i += i
Threading.Thread.Sleep(5000)
Me.txtThread1.Text = i

Next

End Sub

thanks




Re: Windows Forms Data Controls and Databinding Thread restart problem

Rong-Chun Zhang - MSFT

Hi Ather,

For your first question, a stopped or aborted thread cannot be started again. You need to create a new thread and start the new one.

For your second question, access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. And this error occurs during debugging. You need try to use Control.Invoke() to avoid this error. For more information, check this article on MSDN.

Try the following code:

Code Snippet

Public Class Form2

Delegate Sub settext(ByVal str As String)

Private t1 As Thread

Private i As Integer = 0

Private Sub btnT1Start_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnT1Start.Click

If Me.btnT1Start.Text.ToLower() = "start" Then

Me.btnT1Start.Text = "end"

Try

If t1.ThreadState = ThreadState.Unstarted Then

t1.Start()

Me.lblMessage.Text = "Unstarted Start"

ElseIf t1.ThreadState = ThreadState.Suspended Then

t1.Resume()

Me.lblMessage.Text = "Suspended Resume"

ElseIf t1.ThreadState = ThreadState.Aborted Then

t1.Resume() 'no effect

Me.lblMessage.Text = "Aborted resume"

ElseIf t1.ThreadState = ThreadState.Stopped Then

t1.Start()

Me.lblMessage.Text = "Stopped Start"

End If

Catch ex As Exception

MessageBox.Show(ex.ToString())

End Try

ElseIf Me.btnT1Start.Text.ToLower() = "end" Then

Me.btnT1Start.Text = "start"

Try

If t1.ThreadState = ThreadState.Running Then

t1.Suspend()

Me.lblMessage.Text = "Suspand"

Me.txtThread1.Text = i.ToString()

ElseIf t1.ThreadState = ThreadState.WaitSleepJoin Then

t1.Suspend()

Me.lblMessage.Text = "Wait Sleep Join Suspand"

Me.txtThread1.Text = i.ToString()

ElseIf t1.ThreadState = ThreadState.Suspended Then

t1.Suspend()

Me.lblMessage.Text = "Suspand"

Me.txtThread1.Text = i.ToString()

ElseIf t1.ThreadState = ThreadState.Stopped Then

Me.lblMessage.Text = "Stopped"

Me.txtThread1.Text = i.ToString()

t1 = New Thread(New ThreadStart(AddressOf Increment1)) create a new thread

End If

Catch ex As Exception

MessageBox.Show(ex.ToString())

End Try

End If

End Sub

Private Sub Increment1()

For i = 1 To 1000

i += 2

Thread.Sleep(100)

SetTextInstace(i.ToString())

Next

End Sub

Private Sub SetTextInstace(ByVal str As String)

If Me.txtThread1.InvokeRequired Then

Dim pro As New settext(AddressOf SetTextInstace)

Me.Invoke(pro, New Object() {str})

Else

Me.txtThread1.Text = str

End If

End Sub

Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

t1 = New Thread(New ThreadStart(AddressOf Increment1))

End Sub

End Class

Hope this helps.
Best regards.