运行两个并联的异步任务,并收集.NET 4.5结果 [英] Run two async tasks in parallel and collect results in .NET 4.5
问题描述
我已经尝试了一段时间才能得到的东西我想与.NET 4.5的工作将是简单
我要断火在同一时间两个长运行的任务和收集结果
导致在最好的C#4.5(RTM)方法
下面的作品,但我不喜欢它,因为:
- 我想
睡眠
是一个异步方法,因此可以等待
其他方法 - 它只是看起来笨拙与
Task.Run()
- 我不这样即使使用任何新的语言功能在所有的想法!
工作code:
公共静态无效围棋()
{
Console.WriteLine(启动); VAR任务1 = Task.Run(()=>睡眠(5000));
VAR TASK2 = Task.Run(()=>睡眠(3000)); INT totalSlept将= task1.Result + task2.Result; Console.WriteLine(睡总共+ totalSlept将+毫秒);
}私有静态诠释睡眠(INT MS)
{
Console.WriteLine(沉睡+毫秒);
Thread.sleep代码(毫秒);
Console.WriteLine(沉睡+ MS +已完成);
返回毫秒;
}
非工作code:
的更新:这实际工作,是做了正确的方式,唯一的问题是 Thread.sleep代码
的
这code不起作用,因为调用睡眠(5000)
立即开始运行,这样的任务睡眠(1000)
不运行,直到完成。这是真实的,即使睡眠
是异步
,我没有使用等待
或致电。结果
太快了。
我想也许有一种方式来获得一个非运行的任务< T>
通过调用异步
方法,所以我可以再调用开始()
上的两个任务,但我无法弄清楚如何获得任务< T>
从调用异步方法。
公共静态无效围棋()
{
Console.WriteLine(启动); VAR任务1 =睡眠(5000); //块
VAR TASK2 =睡眠(1000); INT totalSlept将= task1.Result + task2.Result; Console.WriteLine(睡了+ totalSlept将+毫秒);
}私有静态异步任务< INT>睡眠(INT MS)
{
Console.WriteLine(沉睡+毫秒);
Thread.sleep代码(毫秒);
返回毫秒;
}
异步任务LongTask1(){...}
异步任务LongTask2(){...}
...
{
任务T1 = LongTask1();
任务T2 = LongTask2();
等待Task.WhenAll(T1,T2);
//现在我们有t1.Result和t2.Result
}
I've been trying for a while to get something I thought would be simple working with .NET 4.5
I want to fire off two long running tasks at same time and collect the
results in in the best C# 4.5 (RTM) way
The following works but I don't like it because:
- I want
Sleep
to be an async method so it canawait
other methods - It just looks clumsy with
Task.Run()
- I don't think this is even using any new language features at all!
Working code:
public static void Go()
{
Console.WriteLine("Starting");
var task1 = Task.Run(() => Sleep(5000));
var task2 = Task.Run(() => Sleep(3000));
int totalSlept = task1.Result + task2.Result;
Console.WriteLine("Slept for a total of " + totalSlept + " ms");
}
private static int Sleep(int ms)
{
Console.WriteLine("Sleeping for " + ms);
Thread.Sleep(ms);
Console.WriteLine("Sleeping for " + ms + " FINISHED");
return ms;
}
Non working code:
Update: This actually works and is the correct way to do it, the only problem is the Thread.Sleep
This code doesn't work because the call to Sleep(5000)
immediately starts the task running so Sleep(1000)
doesn't run until it has completed. This is true even though Sleep
is async
and I'm not using await
or calling .Result
too soon.
I thought maybe there is a way to get a non-running Task<T>
by calling an async
method so I could then call Start()
on the two tasks, but I can't figure out how to get a Task<T>
from calling an async method.
public static void Go()
{
Console.WriteLine("Starting");
var task1 = Sleep(5000); // blocks
var task2 = Sleep(1000);
int totalSlept = task1.Result + task2.Result;
Console.WriteLine("Slept for " + totalSlept + " ms");
}
private static async Task<int> Sleep(int ms)
{
Console.WriteLine("Sleeping for " + ms);
Thread.Sleep(ms);
return ms;
}
async Task LongTask1() { ... }
async Task LongTask2() { ... }
...
{
Task t1 = LongTask1();
Task t2 = LongTask2();
await Task.WhenAll(t1,t2);
//now we have t1.Result and t2.Result
}
这篇关于运行两个并联的异步任务,并收集.NET 4.5结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!