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

前端 未结 3 1900
萌比男神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:11

    When you start a thread this way your function must have one or less parameters. If you specify one parameter, it must be from type Object.

    In your function you can simply cast this object parameter to your datatype:

    private sub startMe(byval param as Object)
         dim b as Boolean = CType(param, Boolean)
         ...
    end sub
    

    When you want to pass multiple parameters, you can put them together into a class like this:

    public class Parameters
         dim paramSTR as String
         dim paramINT as Integer
    end class
    
    private sub startMe(byval param as Object)
         dim p as Parameters = CType(param, Parameters)
         p.paramSTR = "foo"
         p.paramINT = 0
         ...
    end sub
    

    To start your Thread:

    dim t as new Thread(AddressOf startMe)
    dim p as new Parameters
    p.paramSTR = "bar"
    p.oaramINT = 1337
    t.start(p)
    

提交回复
热议问题