c# - Await results of several different asynchronous operations using Task.WhenAny -
c# - Await results of several different asynchronous operations using Task.WhenAny -
i've been searching solution while seems must simple problem. however, every illustration find creates collection of task
s similar 1 (taken how to: extend async walkthrough using task.whenall (c# , visual basic) page on msdn):
ienumerable<task<int>> downloadtasksquery = url in urllist select processurlasync(url);
i unable want phone call different async
methods , await
results... more (but not because doesn't seem anything):
var gettickettasks = new list<task<ienumerable<iticket>>>(); gettickettasks.add(new task(() => model.getticketsasync(eventdate))); gettickettasks.add(new task(() => model.getguestticketsasync(eventdate))); ... gettickettasks.add(new task(() => model.getotherticketsasync(eventdate))); ienumerable<iticket>[] tickets = await task.whenall(gettickettasks);
so how can phone call number of async
methods , await
results using task.whenall
method?
don't utilize task constructor phone call asynchronous method*. phone call these methods, store returned tasks , await
of them task.whenall
:
var gettickettasks = new list<task<ienumerable<iticket>>>(); gettickettasks.add(model.getticketsasync(eventdate)); gettickettasks.add(model.getguestticketsasync(eventdate)); ... gettickettasks.add(model.getotherticketsasync(eventdate)); ienumerable<iticket>[] tickets = await task.whenall(gettickettasks);
in case create task, don't start doesn't run. if did start fire actual asynchronous operations without storing returned task anywhere. means have nil await
. tasks awaiting fire , forget async
operation.
task.whenall
has overload params
alternative be:
var tickets = await task.whenall( model.getticketsasync(eventdate), model.getguestticketsasync(eventdate), model.getotherticketsasync(eventdate));
* or in other case matter.
c# .net asynchronous task-parallel-library async-await
Comments
Post a Comment