简体   繁体   中英

Start a new thread in vb.net when clicking a button

I need help running a function in a new thread when clicking a button. On my frmMain, I have 2 text boxes and a button. There are other controls also, which run in their own threads, but I can't get this particular one to work.

    Private Function IsPortOpen(ByVal Host As String, ByVal PortNumber As Integer) As Boolean
    Dim Client As TcpClient = Nothing
    Try
        Client = New TcpClient(Host, PortNumber)
        Return True
    Catch ex As SocketException
        Return False
    Finally
        If Not Client Is Nothing Then
            Client.Close()
        End If
    End Try
End Function


Private Sub btnCheckPort_Click(sender As Object, e As EventArgs) Handles btnCheckPort.Click

    Dim Port As Integer = tbPortNumber.Text
    Dim Hostname As String = tbHostAddress.Text

    Dim PortOpen As Boolean = IsPortOpen(Hostname, Port)
    Try
        If PortOpen = True Then
            lblPortStatus.Text = "Port " & tbPortNumber.Text & " is Open on " & tbHostAddress.Text
        Else
            lblPortStatus.Text = "Port " & tbPortNumber.Text & " is Not Open on " & tbHostAddress.Text
        End If
    Catch ex As Exception
    End Try

End Sub

If I use this code in the button click sub:

    Private Sub btnCheckPort_Click(sender As Object, e As EventArgs) Handles btnCheckPort.Click

    Dim Port As Integer = tbPortNumber.Text
    Dim Hostname As String = tbHostAddress.Text

    Dim PortOpen As Boolean = IsPortOpen(Hostname, Port)

    manualCheckThread = New Threading.Thread(AddressOf IsPortOpen)
    manualCheckThread.Start()

    Try
        If PortOpen = True Then
            lblPortStatus.Text = "Port " & tbPortNumber.Text & " is Open on " & tbHostAddress.Text
        Else
            lblPortStatus.Text = "Port " & tbPortNumber.Text & " is Not Open on " & tbHostAddress.Text
        End If
    Catch ex As Exception
    End Try

End Sub

I get this error: "Error BC31143 Method 'Private Function IsPortOpen(Host As String, PortNumber As Integer) As Boolean' does not have a signature compatible with delegate 'Delegate Sub ThreadStart()'. Camera Watchdog F:\\dontdig\\frmMain.vb 177"

This just checks to see if a specific port is open at the IP address specified in the textbox.

When creating a new Thread you must pass either a ThreadStart delegate or a ParameterizedThreadStart delegate. ThreadStart is a Sub with no parameters and ParameterizedThreadStart is a Sub with one parameter of type Object . That means that your thread entry method must have a signature that matches one of those two. Your IsPortOpen fails on both the fact that it's a Function and that it has two parameters and neither of them are type Object . If you actually wanted to execute that method on a secondary thread then you'd have to call it from another method that did have a compatible signature and make that your thread entry method.

That said, what would be the point? You're already calling IsPortOpen on the current thread anyway and, even if you weren't, you can't access the value that it returns when calling it on another thread either and, even if you could, you're trying to use that value on the current thread before you could even know that it had been generated. Basically, your multi-threading code doesn't really make any sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM