与参数(S)Task.Run? [英] Task.Run with Parameter(s)?
问题描述
我工作的一个多任务的网络项目,我在 Threading.Tasks
新。我实现了一个简单的 Task.Factory.StartNew()
,我不知道我怎么能与做Task.Run()
下面是基本的代码:
Task.Factory .StartNew(新动作<对象>(
(X)=>
{
//使用'X'
的东西}),RAWDATA);
我看着 System.Threading.Tasks.Task
中的对象浏览器的,我无法找到一个动作< T>
类似的参数。只有动作
这需要无效
参数,并且没有的键入的
有只有两件事情similiar:静态任务运行(操作动作)
和静态任务运行(Func键<任务>功能)
,但不能发布与两个参数(S)
是的,我知道我可以创建一个简单的扩展方法。 ,但我的主要问题是,我们才能把它写在一行与 Task.Run()
?
私人无效RunAsync()
{
字符串参数=嗨;
Task.Run(()=> MethodWithParameter(参数));
}
私人无效MethodWithParameter(字符串参数)
{
//做的东西
}
I'm working on a multi-tasking network project and I'm new on Threading.Tasks
. I implemented a simple Task.Factory.StartNew()
and I wonder how can I do it with Task.Run()
?
Here is the basic code:
Task.Factory.StartNew(new Action<object>(
(x) =>
{
// Do something with 'x'
}), rawData);
I looked into System.Threading.Tasks.Task
in Object Browser and I couldn't find a Action<T>
like parameter. There is only Action
that takes void
parameter and no type.
There are only 2 things similiar: static Task Run(Action action)
and static Task Run(Func<Task> function)
but can't post parameter(s) with both.
Yes, I know I can create a simple extension method for it but my main question is can we write it on single line with Task.Run()
?
private void RunAsync()
{
string param = "Hi";
Task.Run(() => MethodWithParameter(param));
}
private void MethodWithParameter(string param)
{
//Do stuff
}
这篇关于与参数(S)Task.Run?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!