All about Async/Await, System.Threading.Tasks, System.Collections.Concurrent, System.Linq, and more…
Lucian Wischik and I presented an "async clinic" at the MVP Summit in Bellevue this week. The async/await keywords in C# and Visual Basic drastically simplify asynchronous programming, but that of course doesn't mean that using them is without any gotchas: the goal of the discussion was to highlight some of the key areas in which we see developers struggling with asynchronous development and to help provide guidance on avoiding and overcoming those roadblocks. Attached are the slides from the presentation.
Those are very good best practices regarding "async void". I used to have such a confusion whether when to use "async void" or "async Task" for my APIs in my apps. I was unable to find any guidance regarding this issue. After struggling for a while (doing trial-and-error) to decide which one to use, I ended up using "async void" only for top-level methods (of top-level classes too). It works for me so far, and I'm happy it's now confirmed that it's the right thing to do.
Thanks for such a useful guidance! I really appreciate it.
@Maximilian Haru Raditya: You're welcome. I'm glad you found it useful.
Hi Stephen, I have some issue with .net tasks. I tried to leave something on msdn forum but they are having some technical difficulty. I am leave comment here on chance that you might read it and comment.
I have write a webapi. in controller I am trying to start new task. but when api call finishes it finish the new task as well before completeing it. I am testing it from nunit test class. here is the web api controller code.
public Response Get(string id)
var result = Service.Get(id);
var task = Task.Factory.StartNew(() =>
thats what i am getting in console window.
If you have point me in the right direction that would be very helpful.
@haroon: I'm not understanding. That output is what I'd expect to happen. What's the concern?
@haroon If you want to return some result from task and then assign it to method response, you should either wait for task to complete (call task.Result) or write some continuation that will take task result as antecedent and then return it. As Stephen pointed out, output is 100% correct.
I loved the presentation flow. It me even able to illustrate it to my students in a better way. Thanks to both of you