locked
Stop an active thread RRS feed

  • Question

  • Hello

    Here is my program

    Imports System.Threading
    
    Public Class Form1
        Dim s1 As Byte
        Dim s2 As Byte
        Dim s As String
        Dim th As New Thread(AddressOf Me.search)
    
        Private Sub search()
            Dim i As Integer
            For i = s1 To s2
                s = "192.168.0." & i
                Try
                    If My.Computer.Network.Ping(s) Then Me.Invoke(New EventHandler(AddressOf add))
                    Me.Invoke(New EventHandler(AddressOf prog))
    
                Catch ex As Exception
                    Button1.Enabled = True
                    MessageBox.Show("An Error has been occurred , Message" & vbCrLf & ex.Message, "Action", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End Try
            Next
            Try
                Me.Invoke(New EventHandler(AddressOf en))
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub add()
            ListBox1.Items.Add(s)
        End Sub
    
        Private Sub prog()
            Label3.Text = s
            ProgressBar1.Value += 1
        End Sub
    
        Private Sub en()
            Button1.Enabled = True
            MessageBox.Show("Finished", "Action", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub
    
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            s1 = Val(TextBox4.Text)
            s2 = Val(TextBox5.Text)
            Button1.Enabled = False
            ListBox1.Items.Clear()
            ProgressBar1.Value = 0
            ProgressBar1.Maximum = s2 - s1 + 1
            th.Start()
        End Sub
    
    'exit button
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If th.IsAlive = True Then th.Suspend()
            Me.Close()
            Form2.Show()
        End Sub
    
        Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
            If th.IsAlive = True Then th.Suspend()
            If ListBox1.SelectedIndex >= 0 Then
                Dim st As String = ListBox1.Items(ListBox1.SelectedIndex).ToString
                Dim num As Byte = st.LastIndexOf(".")
                st = Mid(st, num + 2, Len(st) - num)
                Form2.TextBox4.Text = Val(st)
                Form2.Show()
                Me.Hide()
            End If
        End Sub
    
    
    
    
    
    End Class
    

    when I click on Button1_Click_1 and my thread starts I can't manage it well.because when the first thread is finished and I click on Button1_Click_1 again I receive this error:

    "Thread is running or terminated; it cannot restart."


    how can I solve this problem?I declare my thread variable public to control it's status but it seems I can't a good control on it.


    Sunday, March 20, 2016 9:45 PM

Answers

  • "Thread is running or terminated; it cannot restart."

    Don't restart an existing thread.  Create and start a new thread.

    Sunday, March 20, 2016 10:19 PM
  • Try this line of code.

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If th.IsAlive = True Then
                th.Abort()
                th = Nothing
                th = New Thread(AddressOf Me.search)
            End If
            s1 = Val(TextBox4.Text)
            s2 = Val(TextBox5.Text)
            Button1.Enabled = False
            ListBox1.Items.Clear()
            ProgressBar1.Value = 0
            ProgressBar1.Maximum = s2 - s1 + 1
            th.Start()
        End Sub

    Monday, March 21, 2016 2:13 PM
  • You have hidden the thrown of errors with this code. 

          Try
                Me.Invoke(New EventHandler(AddressOf en))
            Catch ex As Exception
    
            End Try

    Therefore probably there is something wrong in the process which even you cannot see. 

    Therefore, why do you ask this to us who see even less.


    Success
    Cor

    Monday, March 21, 2016 7:00 PM

All replies

  • "Thread is running or terminated; it cannot restart."

    Don't restart an existing thread.  Create and start a new thread.

    Sunday, March 20, 2016 10:19 PM
  • Try this line of code.

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If th.IsAlive = True Then
                th.Abort()
                th = Nothing
                th = New Thread(AddressOf Me.search)
            End If
            s1 = Val(TextBox4.Text)
            s2 = Val(TextBox5.Text)
            Button1.Enabled = False
            ListBox1.Items.Clear()
            ProgressBar1.Value = 0
            ProgressBar1.Maximum = s2 - s1 + 1
            th.Start()
        End Sub

    Monday, March 21, 2016 2:13 PM
  • You have hidden the thrown of errors with this code. 

          Try
                Me.Invoke(New EventHandler(AddressOf en))
            Catch ex As Exception
    
            End Try

    Therefore probably there is something wrong in the process which even you cannot see. 

    Therefore, why do you ask this to us who see even less.


    Success
    Cor

    Monday, March 21, 2016 7:00 PM