简体   繁体   English

任务。当时在PCL中

[英]Task.WhenAll in PCL

I'm trying to run a few async tasks concurrently inside my Portable Class Library, but the WhenAll method doesn't appear to be supported. 我正在尝试在我的可移植类库中同时运行一些异步任务 ,但似乎不支持WhenAll方法。

My current workaround is to start each task and then await each of them: 我目前的解决方法是启动每个任务,然后等待每个任务:

var task1 = myService.GetData(source1);
var task2 = myService.GetData(source2);
var task3 = myService.GetData(source3);

// Now everything's started, we can await them
var result1 = await task1;
var result1 = await task2;
var result1 = await task3;

Is there something I'm missing? 有什么我想念的吗? Do I need to make do with the workaround? 我是否需要使用解决方法?

Is there something I'm missing? 有什么我想念的吗?

Yes: Microsoft.Bcl.Async . 是的: Microsoft.Bcl.Async

Once you install that package, then you can use TaskEx.WhenAll as a substitute for Task.WhenAll : 安装该软件包后,可以使用TaskEx.WhenAll替代Task.WhenAll

var task1 = myService.GetData(source1);
var task2 = myService.GetData(source2);
var task3 = myService.GetData(source3);

// Now everything's started, we can await them
var results = await TaskEx.WhenAll(task1, task2, task3);

PS Consider using the term parallel for (CPU-bound) parallel processing, and the term concurrent for doing more than one thing at a time. PS考虑使用术语并行 (CPU绑定)并行处理,并且术语并发用于一次执行多个操作。 In this case, the (I/O-bound) tasks are concurrent but not parallel. 在这种情况下,(I / O绑定)任务是并发的但不是并行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM