简体   繁体   中英

WP8 await and async Issue

In my coding, i have 4 tasks. Task A Task B Task C Task 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

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

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 :

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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