How can I cancel a thread?

淺唱寂寞╮ 提交于 2021-01-29 06:23:21

问题


I do something in a thread. But sometimes I don't want to wait till all pings are finished.

How can I cancel a thread?

Can you show me please the code?

Private Sub Start_Button_Click(sender As Object, e As EventArgs) Handles Start_Button.Click
    DoSomething()
End Sub
Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
    THRD.Cancel '<-- Thread cancel!??!???
End Sub
Sub DoSomething()
    Dim THRD As New Thread(Sub()
                               Dim IPArea As String = "192.168.1."
                               Dim LastIP As Integer
                               For LastIP = 0 To 255
                                   Dim TestIP As String = IPArea & CStr(LastIP)
                                   If My.Computer.Network.Ping(TestIP, 10) Then
                                       ListBox1.Items.Add(TestIP)
                                   End If
                               Next
                           End Sub)
    THRD.IsBackground = True
    THRD.Start()
End Sub

回答1:


Here is my working solution and this solution is only to show how to move the THRD as a form level variable to allow stopping it when clicking the cancel button. I added some validations to prevent exceptions.

Public Class Form1
    Private THRD As Threading.Thread

    Private Sub Start_Button_Click(sender As Object, e As EventArgs) Handles Start_Button.Click
        DoSomething()
    End Sub
    Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click

        If THRD IsNot Nothing AndAlso THRD.IsAlive Then
            THRD.Abort()  '<-- Thread cancel!??!???
            THRD = Nothing
            AddToList("Stopped.")
        Else
            AddToList("Thread not running.")
        End If
    End Sub

    Sub DoSomething()
        If THRD IsNot Nothing AndAlso THRD.IsAlive Then
            AddToList("Still working...")
            Exit Sub
        End If

        THRD = New Threading.Thread(Sub()
                                        Dim IPArea As String = "192.168.1."
                                        Dim LastIP As Integer
                                        For LastIP = 0 To 255
                                            Dim TestIP As String = IPArea & CStr(LastIP)
                                            If My.Computer.Network.Ping(TestIP, 10) Then
                                                AddToList(TestIP)
                                            End If
                                        Next
                                        AddToList("Done")
                                    End Sub)
        THRD.IsBackground = True
        THRD.Start()
    End Sub

    ''' <summary>
    ''' Thead-safe add value to list.
    ''' </summary>
    ''' <param name="value">The value.</param>
    Private Sub AddToList(value As String)
        If ListBox1.InvokeRequired Then
            ListBox1.Invoke(Sub() ListBox1.Items.Add(value))
        Else
            ListBox1.Items.Add(value)
        End If
    End Sub
End Class


来源:https://stackoverflow.com/questions/63757557/how-can-i-cancel-a-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!