locked
Asynchronous Programming Design Patterns RRS feed

  • Question

  • I have noticed there have different ways to implement Async design patterns. IAsyncResult or Event based. Which one will be the best one? I have heard the Event based will be going away in C#5.0. Is that true? 
    Wednesday, June 13, 2012 2:34 PM

Answers

  • Hi, 

    which one is the best one?

    It's really what your requirement. you select event-based asynchronous pattern when you have a requirement for support cancellation , support for progress report ...etc which are actually not supported with IAsyncResult pattern (though you can achieve these requirements but required little more work). When you have to chose between IAsyncResult pattern and EAP, IAsyncResult pattern is generally faster and uses fewer resources than EAP.

    Classes with IAsynResult pattern, which were introduced with .Net1.1 are still available with .Net4.5. for eg: Stream.BeginRead. If my requirement is on progress of some operation should report to progress bar, with .Net 4.0 I can do same using EAP (BackgroundWorker) or Task. each has it's own pros and cons and it's really what we are doing as well.

    TAP (Task based asynchronous pattern) which is introduced from .Net 4.0 , pattern offer one method for launching the operation and another method or event for obtaining result by returning Task, Cancellation ,  progress report and wait conventions are also included. (http://visualstudiomagazine.com/articles/2011/08/01/pfcov_task-based-async.aspx). With further simplifying with c#5.0 introduced async/await (series of posts from eric) and async FAQ.

    Hope this helps you....


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".


    • Edited by Kris444 Wednesday, June 13, 2012 5:49 PM
    • Proposed as answer by MasaSam Wednesday, June 13, 2012 6:38 PM
    • Marked as answer by Bob Shen Monday, June 25, 2012 7:25 AM
    Wednesday, June 13, 2012 5:46 PM
  • I have noticed there have different ways to implement Async design patterns. IAsyncResult or Event based. Which one will be the best one? I have heard the Event based will be going away in C#5.0. Is that true? 

    They both have advantages and disadvantages.  The event pattern is typically easier to conceptualize and use, and was "newer" than the original IAsyncResult based pattern.

    .NET 4 added a third, newer model, based on the System.Threading.Task type.  

    Going forward, the entire framework has been updated to use a model which extends the .NET 4 task model, which now works in C# 5 with the new async/await keywords to dramatically simplify asynchronous programming.  Going forward, I suspect this will be the preferred mechanism for all asynchronous programming, as it does make your code look "inside out".


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Bob Shen Monday, June 25, 2012 7:25 AM
    Wednesday, June 13, 2012 4:25 PM

All replies

  • I have noticed there have different ways to implement Async design patterns. IAsyncResult or Event based. Which one will be the best one? I have heard the Event based will be going away in C#5.0. Is that true? 

    They both have advantages and disadvantages.  The event pattern is typically easier to conceptualize and use, and was "newer" than the original IAsyncResult based pattern.

    .NET 4 added a third, newer model, based on the System.Threading.Task type.  

    Going forward, the entire framework has been updated to use a model which extends the .NET 4 task model, which now works in C# 5 with the new async/await keywords to dramatically simplify asynchronous programming.  Going forward, I suspect this will be the preferred mechanism for all asynchronous programming, as it does make your code look "inside out".


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Bob Shen Monday, June 25, 2012 7:25 AM
    Wednesday, June 13, 2012 4:25 PM
  • Hi, 

    which one is the best one?

    It's really what your requirement. you select event-based asynchronous pattern when you have a requirement for support cancellation , support for progress report ...etc which are actually not supported with IAsyncResult pattern (though you can achieve these requirements but required little more work). When you have to chose between IAsyncResult pattern and EAP, IAsyncResult pattern is generally faster and uses fewer resources than EAP.

    Classes with IAsynResult pattern, which were introduced with .Net1.1 are still available with .Net4.5. for eg: Stream.BeginRead. If my requirement is on progress of some operation should report to progress bar, with .Net 4.0 I can do same using EAP (BackgroundWorker) or Task. each has it's own pros and cons and it's really what we are doing as well.

    TAP (Task based asynchronous pattern) which is introduced from .Net 4.0 , pattern offer one method for launching the operation and another method or event for obtaining result by returning Task, Cancellation ,  progress report and wait conventions are also included. (http://visualstudiomagazine.com/articles/2011/08/01/pfcov_task-based-async.aspx). With further simplifying with c#5.0 introduced async/await (series of posts from eric) and async FAQ.

    Hope this helps you....


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".


    • Edited by Kris444 Wednesday, June 13, 2012 5:49 PM
    • Proposed as answer by MasaSam Wednesday, June 13, 2012 6:38 PM
    • Marked as answer by Bob Shen Monday, June 25, 2012 7:25 AM
    Wednesday, June 13, 2012 5:46 PM