Sunday, 15 June 2014

c# - Not slowing down a loop of tasks, and know when result is returned -



c# - Not slowing down a loop of tasks, and know when result is returned -

task<string> runlist(int client) { homecoming pages[client]; } private async void form1_doubleclick(object sender, eventargs e) { (int x = 0; x < listbox1.items.count; x++) { runlist(x); } }

this fly through loop of tasks, how know when results in without compromising speed of loop?

you can await on result of whenall ensure of tasks have completed @ point in code. (it's of import not utilize waitall here, block ui thread.)

private async void form1_doubleclick(object sender, eventargs e) { var tasks = new list<task<string>>(); (int x = 0; x < listbox1.items.count; x++) { tasks.add(runlist(x)); } await task.whenall(tasks); }

the basic thought here start tasks before calling await on them. here simpler illustration 2 tasks:

await task.delay(1000); await task.delay(1000);

this perform first task , then sec task.

var task1 = task.delay(1000); var task2 = task.delay(1000); await task1; await task2;

this start both tasks , go on on after both tasks have finished, allowing run concurrently.

c# task-parallel-library

No comments:

Post a Comment