How to pass multiple parameters in thread in VB

前端 未结 8 1421
面向向阳花
面向向阳花 2020-12-30 05:31

I\'m looking to pass two or more parameters to a thread in VB 2008.

The following method (modified) works fine without parameters, and my status bar gets updated ver

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 05:43

    First of all: AddressOf just gets the delegate to a function - you cannot specify anything else (i.e. capture any variables).

    Now, you can start up a thread in two possible ways.

    • Pass an Action in the constructor and just Start() the thread.
    • Pass a ParameterizedThreadStart and forward one extra object argument to the method pointed to when calling .Start(parameter)

    I consider the latter option an anachronism from pre-generic, pre-lambda times - which have ended at the latest with VB10.

    You could use that crude method and create a list or structure which you pass to your threading code as this single object parameter, but since we now have closures, you can just create the thread on an anonymous Sub that knows all necessary variables by itself (which is work performed for you by the compiler).

    Soo ...

    Dim Evaluator = New Thread(Sub() Me.TestThread(goodList, 1))
    

    It's really just that ;)

提交回复
热议问题