Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem with restarting thread

Problem with restarting thread

Scheduled Pinned Locked Moved Visual Basic
jsonhelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #1

    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
    
    J L 2 Replies Last reply
    0
    • S sohaib_a

      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
      
      J Offline
      J Offline
      Johan Hakkesteegt
      wrote on last edited by
      #2

      If you want to pause and then continue a thread use YourThread.Suspend and YourThread.Resume

      My advice is free, and you may get what you paid for.

      1 Reply Last reply
      0
      • S sohaib_a

        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
        
        L Offline
        L Offline
        LCARS x32
        wrote on last edited by
        #3

        You could also create a new instance of the thread just before starting it. Hope that helps, -Ray

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups