2015-07-11 121 views
0

所以,我是VB.NET的新手,我想創建一個簡單的程序來ping和掃描打開的端口。但是,ping命令就可以了,但掃描的端口不工作就一直說「的ThreadStart不會接受一個參數」,我不知道如何解決它這個.Anyways端口掃描部分的代碼:ThreadStart不接受參數

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    For i As Integer = Start1.Value To End1.Value 
     ' TT= temp threading 
     Control.CheckForIllegalCrossThreadCalls = False 
     Dim TT As New System.Threading.Thread(AddressOf ScanPort) 
     TT.IsBackground = True 
     TT.Start(i) 
    Next 
End Sub 

回答

1

? ...所以讓它接受一個參數?

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    For i As Integer = start1.Value To end1.Value 
     ' TT= temp threading 
     Control.CheckForIllegalCrossThreadCalls = False 
     Dim TT As New System.Threading.Thread(AddressOf ScanPort) 
     TT.IsBackground = True 
     TT.Start(i) 
    Next 
End Sub 

Private Sub ScanPort(ByVal portToScan As Integer) 
    Debug.Print("Scanning Port: " & portToScan) 
    System.Threading.Thread.Sleep(1000) 
End Sub 

隨着選項嚴格上,它會是這樣,而不是:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    For i As Integer = CInt(start1.Value) To CInt(end1.Value) 
     ' TT= temp threading 
     Control.CheckForIllegalCrossThreadCalls = False 
     Dim TT As New System.Threading.Thread(New ParameterizedThreadStart(AddressOf ScanPort)) 
     TT.IsBackground = True 
     TT.Start(i) 
    Next 
End Sub 

Private Sub ScanPort(ByVal portToScan As Object) 
    Dim port As Integer = CType(portToScan, Integer) 
    Debug.Print("Scanning Port: " & port) 
    System.Threading.Thread.Sleep(1000) 
End Sub 
+0

要注意的是這不能當選項嚴格上編譯。 – Ripple

+0

確實...你會將參數類型更改爲Object,並將其轉換爲使用其他變量的正確類型。 –