Passing values with parallel extensions and VB.net
I am looking for an example on how to do the following in VB.net with parallel extensions.
Dim T As Thread = New Thread(AddressOf functiontodowork)
T1.Start(InputValueforWork)
Where I am stuck is about how to pass my InputValueforWork parameter to the task
Dim T As Tasks.Task = Tasks.Task.Create(AddressOf functiontodowork)
Any suggestions and possibly a coding example are appreciated.
Andrew
+1
Middletone
source
to share
3 answers
I solved my question. you need to pass an array with values.
Dim A(0) as Int32
A(0) = 1
Tasks.Task.Create(AddressOf TransferData, A)
+1
Middletone
source
to share
Not necessarily the most helpful answer I know, but in C # you can do it with a closure:
var T = Tasks.Task.Create( () => functionToDoWork(SomeParameter) )
0
Paul betts
source
to share
The real problem here is that VB 9 doesn't supportAction<T>
, only Funcs
You can get around this limitation by having a helper in C # like:
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
0
Mauricio scheffer
source
to share