Problem with restarting thread
-
I have function 'startthread' that points to the thread 'thread1' which is started when I click on button1. When i click on button2,it is supposed to stop the thread.I want to restart the thread1 but when i click on button 2 again it gives ThreadStateException saying 'Thread is running or terminated; it cannot restart' What am i doing wrong here?
''To Start the thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds1 As New DataSet
Dim i As Integer = 0
ds1 = clsDB.getData("Select * from Readers_test")Dim s As String = ds1.Tables(0).Rows(0)("Reader\_Name").ToString + ":" + ds1.Tables(0).Rows(0)("IP\_Address").ToString + ":" + ds1.Tables(0).Rows(0)("Port\_No").ToString 'thread1.IsBackground = True thread1.Start(s) ''I get exceptio here when I try to restart the thread End Sub
''To Stop the thread
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
api.TcpCloseConnect() If thread1.IsAlive Then thread1.Abort() End If setValue(Me.TextBox3, "Stopped") End Sub Private Sub startthread(ByVal state As Object) Dim timer1 As New System.Timers.Timer AddHandler timer1.Elapsed, AddressOf scan Dim st As String = state Dim status As Integer = 2 Dim param\_arr() As String = st.Split(":") Dim Location As String = param\_arr(0) Dim ReaderIP As String = param\_arr(1) Dim Port As String = param\_arr(2) Dim ReaderPort As Integer = Integer.Parse(param\_arr(2)) status = api.TcpConnectReader(ReaderIP, ReaderPort) If status = 0 Then setValue(Me.TextBox1, "Connected") api.ClearIdBuf() timer1.Interval = 1000 loc = Location clearbuf = 0 timer1.Start() Else setValue(Me.TextBox1, "Not Connected") End If End Sub Public Sub scan(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Dim status As Integer = 2 Dim i As Integer, j As Integer Dim IsoBuf As Byte(,) = New Byte(99, 13) {} Dim tag\_cnt As Byte = 0 Dim s As String = "" Dim s1 As String = "" Dim antno As Integer = 5 Dim tagstatus As String
-
I have function 'startthread' that points to the thread 'thread1' which is started when I click on button1. When i click on button2,it is supposed to stop the thread.I want to restart the thread1 but when i click on button 2 again it gives ThreadStateException saying 'Thread is running or terminated; it cannot restart' What am i doing wrong here?
''To Start the thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds1 As New DataSet
Dim i As Integer = 0
ds1 = clsDB.getData("Select * from Readers_test")Dim s As String = ds1.Tables(0).Rows(0)("Reader\_Name").ToString + ":" + ds1.Tables(0).Rows(0)("IP\_Address").ToString + ":" + ds1.Tables(0).Rows(0)("Port\_No").ToString 'thread1.IsBackground = True thread1.Start(s) ''I get exceptio here when I try to restart the thread End Sub
''To Stop the thread
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
api.TcpCloseConnect() If thread1.IsAlive Then thread1.Abort() End If setValue(Me.TextBox3, "Stopped") End Sub Private Sub startthread(ByVal state As Object) Dim timer1 As New System.Timers.Timer AddHandler timer1.Elapsed, AddressOf scan Dim st As String = state Dim status As Integer = 2 Dim param\_arr() As String = st.Split(":") Dim Location As String = param\_arr(0) Dim ReaderIP As String = param\_arr(1) Dim Port As String = param\_arr(2) Dim ReaderPort As Integer = Integer.Parse(param\_arr(2)) status = api.TcpConnectReader(ReaderIP, ReaderPort) If status = 0 Then setValue(Me.TextBox1, "Connected") api.ClearIdBuf() timer1.Interval = 1000 loc = Location clearbuf = 0 timer1.Start() Else setValue(Me.TextBox1, "Not Connected") End If End Sub Public Sub scan(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Dim status As Integer = 2 Dim i As Integer, j As Integer Dim IsoBuf As Byte(,) = New Byte(99, 13) {} Dim tag\_cnt As Byte = 0 Dim s As String = "" Dim s1 As String = "" Dim antno As Integer = 5 Dim tagstatus As String
If you want to pause and then continue a thread use
YourThread.Suspend
andYourThread.Resume
My advice is free, and you may get what you paid for.
-
I have function 'startthread' that points to the thread 'thread1' which is started when I click on button1. When i click on button2,it is supposed to stop the thread.I want to restart the thread1 but when i click on button 2 again it gives ThreadStateException saying 'Thread is running or terminated; it cannot restart' What am i doing wrong here?
''To Start the thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds1 As New DataSet
Dim i As Integer = 0
ds1 = clsDB.getData("Select * from Readers_test")Dim s As String = ds1.Tables(0).Rows(0)("Reader\_Name").ToString + ":" + ds1.Tables(0).Rows(0)("IP\_Address").ToString + ":" + ds1.Tables(0).Rows(0)("Port\_No").ToString 'thread1.IsBackground = True thread1.Start(s) ''I get exceptio here when I try to restart the thread End Sub
''To Stop the thread
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
api.TcpCloseConnect() If thread1.IsAlive Then thread1.Abort() End If setValue(Me.TextBox3, "Stopped") End Sub Private Sub startthread(ByVal state As Object) Dim timer1 As New System.Timers.Timer AddHandler timer1.Elapsed, AddressOf scan Dim st As String = state Dim status As Integer = 2 Dim param\_arr() As String = st.Split(":") Dim Location As String = param\_arr(0) Dim ReaderIP As String = param\_arr(1) Dim Port As String = param\_arr(2) Dim ReaderPort As Integer = Integer.Parse(param\_arr(2)) status = api.TcpConnectReader(ReaderIP, ReaderPort) If status = 0 Then setValue(Me.TextBox1, "Connected") api.ClearIdBuf() timer1.Interval = 1000 loc = Location clearbuf = 0 timer1.Start() Else setValue(Me.TextBox1, "Not Connected") End If End Sub Public Sub scan(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Dim status As Integer = 2 Dim i As Integer, j As Integer Dim IsoBuf As Byte(,) = New Byte(99, 13) {} Dim tag\_cnt As Byte = 0 Dim s As String = "" Dim s1 As String = "" Dim antno As Integer = 5 Dim tagstatus As String