Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple parameters in thread in VB

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 very cool-y. But I can't seem to make it work with one, two or more parameters.

This is the pseudo code of what I'm thinking should happen when the button is pressed:

Private Sub Btn_Click() 

Dim evaluator As New Thread(AddressOf Me.testthread(goodList, 1))
evaluator.Start()

Exit Sub

This is the testthread method:

Private Sub testthread(ByRef goodList As List(Of OneItem), ByVal coolvalue As Integer)

    StatusProgressBar.Maximum = 100000
    While (coolvalue < 100000)
        coolvalue = coolvalue + 1
        StatusProgressBar.Value = coolvalue
        lblPercent.Text = coolvalue & "%"
        Me.StatusProgressBar.Refresh()
    End While

End Sub
like image 941
elcool Avatar asked Oct 25 '10 19:10

elcool


4 Answers

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 ;)

like image 83
Dario Avatar answered Nov 14 '22 19:11

Dario


Something like this (I'm not a VB programmer)

Public Class MyParameters
    public Name As String
    public Number As Integer
End Class



newThread as thread = new Thread( AddressOf DoWork)
Dim parameters As New MyParameters
parameters.Name = "Arne"
newThread.Start(parameters);

public shared sub DoWork(byval data as object)
{
    dim parameters = CType(data, Parameters)

}
like image 20
jgauffin Avatar answered Nov 14 '22 18:11

jgauffin


Dim evaluator As New Thread(Sub() Me.testthread(goodList, 1))
With evaluator
.IsBackground = True ' not necessary...
.Start()
End With
like image 4
Manuel Alves Avatar answered Nov 14 '22 18:11

Manuel Alves


Well, the straightforward method is to create an appropriate class/structure which holds all your parameter values and pass that to the thread.

Another solution in VB10 is to use the fact that lambdas create a closure, which basically means the compiler doing the above automatically for you:

Dim evaluator As New Thread(Sub()
                                testthread(goodList, 1)
                            End Sub)
like image 3
Konrad Rudolph Avatar answered Nov 14 '22 17:11

Konrad Rudolph