How can I create a new thread AddressOf a function with parameters in VB?

前端 未结 3 1910
萌比男神i
萌比男神i 2020-12-21 00:28

When option strict is OFF, works fine. ON, I get overload resolution failure:

Dim _thread1 As Thread

Private Sub test2(boolTest As Boolean)
    \' Do someth         


        
3条回答
  •  失恋的感觉
    2020-12-21 01:08

    You should follow al-eax's answer, but another way would be to not pass parameters in the Thread.Start() function at all, but rather evaluate it in the test sub...

    Dim _thread1 As Thread
    
    Private Sub test()
        If someTest = True then    
            _thread1 = New Thread(AddressOf test2)
            _thread1.Start()
        End If
    End Sub
    
    Private Sub test2()
        /.../
    End Sub
    

    ...or declare it as a global variable...

    Dim _thread1 As Thread
    Dim boolTest As Boolean
    
    Private Sub test()
        boolTest = True
    
        _thread1 = New Thread(AddressOf test2)
        _thread1.Start()
    End Sub
    
    Private Sub test2()
        If boolTest = True Then
            /.../
        End If
    End Sub
    

提交回复
热议问题