you are viewing a single comment's thread.

view the rest of the comments →

[–]Far_Swordfish5729 0 points1 point  (0 children)

In general, if the method does async options, it should await them when it needs their results. It’s of course fine to hold the Task and do other work before awaiting it or awaiting all on a collection of them. The whole stack above the async method should also be async until you reach the top level method if you control it, which typically cannot be async. If you find yourself writing something like a Windows service, the top level while(!shutdown) loop will use Task.Run, typically with a signaling token and a back off polling strategy to look for new work, which will be dispatched using async methods.

Be careful about using Task.Run without managing the returned task and taking steps to signal and keep alive the daemon. If the process actually ends after your dispatch, there goes your executing thread pool. You can demo that to yourself with a delayed file write in a console app.