c# - Task.Factory.StartNew with async lambda and Task.WaitAll -
i'm trying use task.waitall
on list of tasks. thing tasks async lambda breaks tasks.waitall
never waits.
here example code block:
list<task> tasks = new list<task>(); tasks.add(task.factory.startnew(async () => { using (dbcontext = new databasecontext()) { var records = await dbcontext.where(r => r.id = 100).tolistasync(); //do long cpu process here... } } task.waitall(tasks); //do more stuff here
this doesn't wait because of async lambda. how supposed await i/o operations in lambda?
task.factory.startnew
doesn't recognise async
delegates there no overload accepts function returning task
.
this plus other reasons (see startnew dangerous) why should using task.run
here:
tasks.add(task.run(async () => ...
Comments
Post a Comment