如果在 VB.NET 中超时,如何停止/登上线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17607625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:14:37  来源:igfitidea点击:

How to stop/aboard threads if timeout in VB.NET

.netvb.netmultithreadingthreadpool

提问by Dennis

I am working on a code which will create a series threads (e.g: Function GetMACAddress(IPAddr as string) as string). Each thread job will take different execution time to process (will take way longer if IP not occupied by any PC & no valid MAC Address will be return at the end).

我正在编写将创建一系列线程的代码(例如:)Function GetMACAddress(IPAddr as string) as string。每个线程作业将花费不同的执行时间来处理(如果 IP 没有被任何 PC 占用,并且最后将没有有效的 MAC 地址将返回,则将花费更长的时间)。

How can I manage & monitor each thread in such a way so that I can stop/abort at anytime if it still yet to be completed after a specified timeout period (say Timeout = 100ms)? In my case I'll need to scan from 192.168.1.1 to 192.168.1.255

如何以这种方式管理和监视每个线程,以便在指定的超时期限(例如超时 = 100 毫秒)后仍未完成时我可以随时停止/中止?就我而言,我需要从 192.168.1.1 扫描到 192.168.1.255

What sort of threading coding architecture I should using here?

我应该在这里使用什么样的线程编码架构?

回答by David -

Try the following code

试试下面的代码

Imports System

Public Class TimedThread

    Dim WithEvents Timer1 As Timers.Timer
    Dim Thread1 As Threading.Thread

    Dim Timeout1 As Integer

    Sub New(ByVal Timeout1 As Integer, ByVal ThreadStart1 As Threading.ThreadStart)
        Me.Timeout1 = Timeout1

        If Timeout1 > 0 Then
            Timer1 = New Timers.Timer(Timeout1)
        End If

        Thread1 = New Threading.Thread(ThreadStart1)
    End Sub

    Public Sub Start()
        If Not Thread1 Is Nothing Then
            Thread1.Start()
        End If

        If Timeout1 > 0 Then
            Timer1.Enabled = True
        End If
    End Sub

    Private Sub Timer1_Elapsed() Handles Timer1.Elapsed
        If Thread1.ThreadState = Threading.ThreadState.Running Then
            Thread1.Abort()
            Timer1.Enabled = False

            'Remove this line after testing
            MsgBox("Thread aborted")
        End If
    End Sub

    Public Sub Dispose()
        If Not Timer1 Is Nothing Then
            If Thread1.ThreadState = Threading.ThreadState.Running Then
                Thread1.Abort()
            End If

            Timer1.Dispose()
        End If
    End Sub

End Class

to test the code, add these lines inside your form

要测试代码,请在表单中添加这些行

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim Timeout1 As Integer = 1000
    Dim TimedThread1 As New TimedThread(Timeout1, AddressOf TestSub)

    TimedThread1.Start()
End Sub

Private Sub TestSub()
    Dim i, a As Integer

    For i = 0 To 1000000000
        a += 1
    Next

    MsgBox("Operation complete")
End Sub

This demonstration is for a single thread. If you want to use multiple threads, use an array of the TimedThread Class.

这个演示是针对单线程的。如果要使用多个线程,请使用 TimedThread 类的数组。

To set for example an IP address as a input parameter and get the MAC address as a return value, you can use the following approach.

例如,要将 IP 地址设置为输入参数并获取 MAC 地址作为返回值,您可以使用以下方法。

Public Structure NetInfo
    Dim IP_Address As String
    Dim MAC_Address As String

    Sub New(ByVal IP_Address As String, ByVal MAC_Address As String)
        Me.IP_Address = IP_Address
        Me.MAC_Address = MAC_Address
    End Sub
End Structure

Private Event ReturnValues(ByVal ReturnInfo1 As NetInfo)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim Timeout1 As Integer = 0
    Dim TimedThread1 As New TimedThread(Timeout1, Sub() GetMacAddress("192.168.0.1"))

    TimedThread1.Start()
End Sub

Private Sub GetMacAddress(ByVal IP_Address1 As String)
    'Processes for getting the MAC address based on the IP address.
    '
    '
    Dim MAC_Address1 As String = "Test return value"
    Dim ReturnInfo1 As New NetInfo(IP_Address1, MAC_Address1)

    RaiseEvent ReturnValues(ReturnInfo1)
End Sub

Private Sub ThreadReturnValues(ByVal ReturnInfo1 As NetInfo) Handles Me.ReturnValues
    MsgBox("IP = " & ReturnInfo1.IP_Address & " MAC = " & ReturnInfo1.MAC_Address)
End Sub