c# - Async - Which of these is correct -
from below 2 scenario(s), 1 of them correct way of doing asynchronous programming in c#?
scenario-1
public async task<t1> addsomethingasync(param) { return await someotherfunctionfromthirdpartylibraryforioasync(param); }
then
list<task> tasks=new list<task>(); foreach(var task in fromalltasks()) { tasks.add(addsomethingasync(task)); } await task.whenall(tasks.asparallel());
scenario-2
public async task<t1> addsomethingasync(param) { return someotherfunctionfromthirdpartylibraryforioasync(param); }
then
list<task> tasks=new list<task>(); foreach(var task in fromalltasks()) { tasks.add(someotherfunctionfromthirdpartylibraryforioasync(task)); } await task.whenall(tasks.asparallel());
the difference between 2 is, later not having await
keyword inside addsomethingasync
function.
so here update - want know achieve is, tasks should executed in parallel , asynchronously. (my thinking in scenario-1, call awaited inside addsomethingasync , hurt @ upper layer blocking next loop execute. confirm
scenario 3
public task<t1> addsomethingasync(param) { return someotherfunctionfromthirdpartylibraryforioasync(param); }
then
list<task> tasks=new list<task>(); foreach(var task in fromalltasks()) { tasks.add(someotherfunctionfromthirdpartylibraryforioasync(task)); } await task.whenall(tasks);
if not awaiting - don't need async keyword. doing asparallel nothing in case too.
Comments
Post a Comment