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

时间:2013-07-12 04:49:34

标签: .net vb.net multithreading threadpool

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

我该如何管理&以这种方式监视每个线程,以便在指定的超时时间(比如Timeout = 100ms)之后仍然可以完成时,我可以随时停止/中止?在我的情况下,我需要从192.168.1.1扫描到192.168.1.255

我应该在这里使用哪种线程编码架构?

1 个答案:

答案 0 :(得分:6)

尝试以下代码

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

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

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

此演示适用于单个线程。如果要使用多个线程,请使用TimedThread类的数组。

要将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