views:

441

answers:

3

I am looking for an example of how to do the following in VB.net with Parallel Extensions.

Dim T As Thread = New Thread(AddressOf functiontodowork)
T1.Start(InputValueforWork)

Where I'm getting stuck is on how to pass into the task my parameter InputValueforWork

Dim T As Tasks.Task = Tasks.Task.Create(AddressOf functiontodowork)

Any suggests and possibly a coding example would be welcome.

Andrew

A: 

Not necessarily the most helpful answer I know, but in C# you could do this with a closure:

var T = Tasks.Task.Create( () => functionToDoWork(SomeParameter) )
Paul Betts
A: 

The real problem here is that VB 9 doesn't support Action<T>, only Funcs

You can work around this limitation by having a helper in C#, like this:

public class VBHelpers {
    public static Action<T> FuncToAction<T>(Func<T, object> f) {
        return p => f(p);
    }
}

Then you use it from VB like this:

Public Sub DoSomething()
    Dim T As Task = Task.Create(VBHelpers.FuncToAction(Function(p) FunctionToDoWork(p)))
End Sub

Public Function FunctionToDoWork(ByVal e As Object) As Integer
    ' this does the real work
End Function
Mauricio Scheffer
+1  A: 

I solved my own question. you have to pass in an array with the values.

Dim A(0) as Int32
A(0) = 1
Tasks.Task.Create(AddressOf TransferData, A)
Middletone