I am looking for an example of how to do the following in VB.net with Parallel Extensions.
我正在寻找一个如何在VB.net中使用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
我遇到的问题是如何将任务传递给我的参数InputValueforWork
Dim T As Tasks.Task = Tasks.Task.Create(AddressOf functiontodowork)
Any suggests and possibly a coding example would be welcome.
任何建议和可能的编码示例都将受到欢迎。
Andrew
3 个解决方案
#1
1
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)
#2
0
Not necessarily the most helpful answer I know, but in C# you could do this with a closure:
不一定是我知道的最有用的答案,但在C#中你可以用一个闭包来做到这一点:
var T = Tasks.Task.Create( () => functionToDoWork(SomeParameter) )
#3
0
The real problem here is that VB 9 doesn't support Action<T>
, only Funcs
这里真正的问题是VB 9不支持Action
You can work around this limitation by having a helper in C#, like this:
您可以通过在C#中使用帮助程序来解决此限制,如下所示:
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:
然后你从VB使用它像这样:
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
#1
1
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)
#2
0
Not necessarily the most helpful answer I know, but in C# you could do this with a closure:
不一定是我知道的最有用的答案,但在C#中你可以用一个闭包来做到这一点:
var T = Tasks.Task.Create( () => functionToDoWork(SomeParameter) )
#3
0
The real problem here is that VB 9 doesn't support Action<T>
, only Funcs
这里真正的问题是VB 9不支持Action
You can work around this limitation by having a helper in C#, like this:
您可以通过在C#中使用帮助程序来解决此限制,如下所示:
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:
然后你从VB使用它像这样:
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