[英]WP8 await and async Issue
In my coding, i have 4 tasks. 在我的编码中,我有4个任务。 Task A Task B Task C Task D
任务A任务B任务C任务D
Task D need to be executed after finish of Task A ,B &C Task A , Task B ,Task C are Independence and can execute at the same time 任务D,任务B和任务C完成后需要执行任务D,任务B,任务C和任务C是独立的,并且可以同时执行
i am now using a sequence approach to perform the action, it take very slow 我现在正在使用序列方法来执行操作,这需要很慢
await Task A;
await Task B;
await Task C;
await Task D;
I would like to know if i can make it run concurrently and After finished Task A,B,C ,it will run Task D 我想知道是否可以使其同时运行,并且在完成任务A,B,C之后,它将运行任务D
Thanks for your help . 谢谢你的帮助 。
await Task.WhenAll(new Task[]{Task A, Task B, Task C})
await Task D
You can assign the tasks to do the necessary work but not await as they start doing that. 您可以分配任务来执行必要的工作,但是在任务开始执行时不要等待。
Create a variable and assign it to the task that comes back: 创建一个变量并将其分配给返回的任务:
var taskA = A();
var taskB = B();
var taskC = C();
At this point, they all started in the background and only afterwards we wait them to finish: 至此,它们都在后台启动,直到之后我们才等待它们完成:
await taskA;
await taskB;
await taskC;
And after the completion of A, B, C, run and wait for it to complete D : 在完成A,B,C之后,运行并等待其完成D:
await D();
How about using Thread? 如何使用线程?
List<Thread> tmpThread = new List<Thread>();
tmpThread.Add(new Thread(new ThreadStart(A));
tmpThread.Add(new Thread(new ThreadStart(B));
tmpThread.Add(new Thread(new ThreadStart(C));
tmpThread.Add(new Thread(new ThreadStart(D));
tmpThread[0].Start();
tmpThread[1].Start();
tmpThread[2].Start();
tmpThread[3].Start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.