Windows Store apps for Absolute Beginners with C#

Part 12: Working with Async Methods in the Windows Runtime

Download this episode

Download Video


The new C# 5.0 async features are explained in this lesson, including the pattern we can follow to utilize the Windows Runtime async methods to keep our apps fast and fluid.



Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • RafaelMiceli

      Hi Bob!

      First of all, great series! It's really helping a LOT to develop for windows 8 and also to meet the new features on C# 5.0 and VS2012!

      And second, when a method is top level in C#?

      Thank's a lot

      PS: when will you make a series about Win 8 application with Javascript, html 5 and CSS 3? It would be great too!!!

    • BobTabor

      @RafaelMiceli: "top level method" ... I didn't know how to express it ... everyone calls them by different names.  The Microsoft Application Architecture Guide would categorize these sorts of concerns as methods in the "Application Facade" sub-layer of the "Business Logic Layer" ... what I call the "Domain Layer".  Some refer to the "Application Facade" as a "Services" layer (but not a "Web Services" layer. *Sigh*).  

      Basically, it's any coarse-grained method that coordinates / composes a functional concern of the application.  So, a "CreateOrder" method, which may call other finer-grained methods in multiple classes and coordinates their efforts so that an order is created in the system.  You don't make these methods async insomuch that they are performing the work for the UI layer classes.  You want the UI to just call the method in the "application facade" and let it do its magic (network, file, data, etc. stuff) and return control back to the UI as soon as possible.  Hope that makes sense.


      Edit on 1/3/2013: After further consideration, I realize I made this much more confusing than necessary.  In short, yes, void should only be used for a "top level method", but let me explain that a bit more.  It is typically applied to something that doesn't need a response ... so a "fire and forget" async operation.  The caller never waits for the result, and doesn't even know if it finishes.  This limits its usefulness to those "top level methods" that are a boundary between non-async and async code.  A great example of that is an user-interface event-handler ... button_click() method should/must be void, however it *may* be marked as async because it awaits other async operations in its body.  Hopefully that clears up my previous answer.

    • RafaelMiceli

      Wow! It's really more complicated than i thought!

      Certainly helped!

      Thanks for the answer!

      I really like your explanations.

      I met your teach way in and i had to say, you can explain in a way that's really easy understand some very tough topics and i hope i can teach like you someday!

      Keep the great work and thanks for the other lessons in channel9 too!!!

    • BobTabor

      @RafaelMiceli: re: more complicated ... Sorry, I had "architecture" on the brain since I'm creating videos for my own website (lvs) on that topic right now.  That may have been an over-explanation.

      re: I hope I can teach ... Thanks for the compliment ... I hope you don't or else I'll be out of a job.  Wink  j/k ... thank you for the kind words and good luck!

    • RafaelMiceli

      Don't Sorry!

      Really helped. Big Smile



    • BobTabor

      @RafaelMiceli: Following up on that thought ... if you want to explain things clearly, you need to do two things.  (1) Start by trying to understand a given topic at the most micro level, then once you get to the point where you've exhausted the tiny details (at least, to your own satisfaction) then come back up and try to understand the topic at the most macro level.  In most cases, that means understanding where a given concept, technology, technique, etc. fits in to the "big picture".  (2) Find ways to map highly conceptual ideas (like almost everything in software development) to the real world.  This is a by-product of completely understanding something ... you build mappings / mental models to other similar ideas, or in entirely different disciplines (art, music, science, medicine, engineering, nature, etc.)  

      For me, treating things anthropomorphically helps in this process.  "I'm a class that needs to persist my data.  I'm willing to work with any other class to accomplish this as long as that class meets my terms and conditions.  So, if you want to work with me, then you have to implement this contract, this *Interface*.  Honestly, I don't care who you are or how you do your persistence thingy ... so don't tell me about all that.  I just want to know that I can call these methods and pass you this stuff and you'll take care of it."  That helps me understand then explain the role of interfaces in the Repository pattern, for example.

      Anyone can take something simple and express it in such a way that it seems more difficult than it really is.  It takes hard work to take something hard, break it down, and express it in an easily digested way.  

    • RafaelMiceli

      And I'll Work hard as you sad to it!

      I'm studying a lot and studying all the tiny details of the topics to understand the macro level as you sad! I already have watched a bunch of your series in your site learnvisualstudio and also here in channel9, and i learned with you that if you want to understand something, is to understand all the details!

      I realised that in your C# fundamental series, in your site, when you explain about the Lambda Expression, and to explain that you first explained three topics! Anonymous methods, Delegates and Extension methods, and after that i started to always dive deep in the topics to see what some people don't see!

      Thanks a lot for this HUGE tip!

      It's a lesson for life! Thanks!

    • RafaelMiceli

      And sorry with the bad english, I'm brazilian, and i'm trying to speak and type better! Lol.

    • BobTabor

      @RafaelMiceli: You're doing fine.  One last thing ... I draw a lot of insight / encouragement from this author / blogger ... maybe this is something you will find interesting as well:

    • RafaelMiceli

      Thank's again Bob! And i thanked you a lot today! So to don't lose the habit, Thanks again LOL

      Ps: Cal's new Project from november 7° is the best! Smiley

    • LukePuplett

      I'm really sorry if you've answered this a thousand times, but is that colour scheme available somewhere? (and does it make XML and other documents look as sexy?)

    • Phillip Haydon

      @LukePuplett - I'm pretty sure that's just the default dark theme from Visual Studio 2012.

      Tools > Options > General > Color Theme > Dark

    • BobTabor

      @LukePuplett: Yes, what @Phillip Haydon said (thanks!).  That color scheme is the default for the EXPRESS edition of Visual Studio, but in the PRO edition or greater you must select this theme per @Phillip Haydon 's instructions.  I also discuss and demo this in the very first video in this series.  Hope that helps!

    • Itignition

      Clearly explained and well presented. 

    • BobTabor

      @Itignition: Thank you.  I wish I could do this one over ... I've studied async a lot since I recorded this video and I would have approached it differently, and I understand the internals a bit better now.  Oh well ... the intent is the same, just some of the details are suspect.

    • EatDrinkCode

      Can we use async keyword in all methods we write? For example, we can use try-catch block in every method or class without any restriction (as far as I know). Is there any restriction preventing us from using async keyword?

    • BobTabor

      @EatDrinkCode: Yes and no.  You can await anything, but it's only really useful for tasks that are (1) long running, and (2) the task is I/O-bound.  By "I/O-bound" I mean that your application's execution is blocked by some input / output -- waiting on a file to load, waiting on a network resource, etc.  HOWEVER just because you can doesn't mean you should.  When you await, you incur a small penalty whilst the infrastructure that swaps contexts and such sets up for the await -- which it may not even need!  You would be better off not using it indiscriminately.  


      My advice: "Build it, built it right, build it fast."  Worry about optimization LAST, and only attempt to optimize once you have empirical evidence that you need it, and WHERE in your app you need it (use a utility like dotTrace, JetBrain's performance and memory profiler).


      BY the way, if you have a long running, processor intensive "CPU-bound" process (i.e., you're trying to sequence the human genome) using await won't help much.  You would be better off learning about the Task Parallel Library added in .NET Framework 4.0.  It's a friendly layer on top of the complexity of working with threading and such.

    • BobTabor

      Opening comments

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.