Asynchronous Programming in C# - Part 1 Presenter: Vikash Kumar, Mindfire Solutions (28/07/2014)
About Me MCTS-70-515 - Microsoft .NET 4.0, Web App Development MCP:70-483: MS - Programming in C# Skills: MVC, Asp.Net WebForms, C#, EntityFramework, OpenAccess, TypeScript, SEO, jQuery, KendoUI, Lucene.Net, Asynchronous programming, Sql Server. Connect Me: Blog :- http://vikutech.blogspot.in/ Twitter :- https://twitter.com/viku85 LinkedIn :- http://www.linkedin.com/pub/vikash-kumar/11/4b5/625 Google+ :- https://plus.google.com/108754146104237310375 Contact Me: Email :- vikashk@mindfiresolutions.com / viku85@gmail.com Skype :- mfsi_vikash
Agenda  What is asynchronous?  Asynchronous programming options and history  Thread and ThreadPool  Asynchronous Programming Model (APM)  Event-based Asynchronous Pattern (EAP)  Task – Basics – Exception handling – Relationships
What is asynchronous? Image Source: http://www.webopedia.com/ To do more than one thing at a time. asyn = not with chronos = time
Asynchronous programming options and history  Thread  Asynchronous Programming Model (APM)  Event-Based Asynchronous Pattern (EAP)  BackgroundWorker  Task  Task Parallel Library - DataFlow  Task Parallel Library – Parallel APIs  Thread → APM → EAP → TAP
Thread and ThreadPool  Basic building block for async programming.  Parameterize thread, came with .NET 2.0 Image source:http://johnpaulmorabito.blogspot.in/
APM and EAP  APM depends upon Begin and End methods.  Callbacks are dependent on IAsyncResult.  EAP came with .NET 2.0 which automatically handles synchronization context.  BackgroundWorker is an example of EAP based programming.  EAP can be seen while adding web reference which automatically creates with proxies classes.
APM and EAP
Task  A task is an abstraction of unit of work to run asynchronously.  Ways to execute: new Task(Speak).Start(), Task.Factory.StartNew(Speak), Task.Run(Speak)  Use FromAsync when an API has BeginXXX/EndXXX.  Task can be canceled through CancellationToken and checked with ThrowIfCancellationRequested.  IProgress<T> came with 4.5 which can be used to show status
Task error handling  Task are completed with following state: Ran to Completion, Canceled and Faulted.  Task uses AggregateException to wrap up any other exception occurs which can be found under inner exception.  AggregateException comes with Flatten method to check all exceptions.  AggregateException also comes with Handle method which accepts predicate to handle errors.  ThrowUnobservedTaskExceptions to configure to shallow exception.
Cancellation FromAsync
Progress bar Exception handling
Task Relationships Task Task Task Task Child Tasks Task Task Task Chained Task
Task Relationships...  ContinueWhenXXX can be used to chain up tasks with TaskContinuationOptions enum for various conditions.  TaskCreationOptions.AttachedToParent is used to create child tasks.  Creating child task will reflect exception in parent task if exception occurs.  Task.Run uses TaskCreationOptions.DenyChildAttach by default which will act as nested task.
Task Relationship
Presenter: XXXX XXXX, Mindfire Solutions Question and Answer
References  Pro Asynchronous Programming with .NET  Asynchronous Programming Patterns  Asynchronous Programming in .NET: I'll Call You Bac (For definition)
Presenter: Vikash Kumar, Mindfire Solutions Thank you
www.mindfiresolutions.com https://www.facebook.com/MindfireSolutions http://www.linkedin.com/company/mindfire-solutions http://twitter.com/mindfires

Asynchronous Programming in C# - Part 1

  • 1.
    Asynchronous Programming inC# - Part 1 Presenter: Vikash Kumar, Mindfire Solutions (28/07/2014)
  • 2.
    About Me MCTS-70-515 -Microsoft .NET 4.0, Web App Development MCP:70-483: MS - Programming in C# Skills: MVC, Asp.Net WebForms, C#, EntityFramework, OpenAccess, TypeScript, SEO, jQuery, KendoUI, Lucene.Net, Asynchronous programming, Sql Server. Connect Me: Blog :- http://vikutech.blogspot.in/ Twitter :- https://twitter.com/viku85 LinkedIn :- http://www.linkedin.com/pub/vikash-kumar/11/4b5/625 Google+ :- https://plus.google.com/108754146104237310375 Contact Me: Email :- vikashk@mindfiresolutions.com / viku85@gmail.com Skype :- mfsi_vikash
  • 3.
    Agenda  What is asynchronous?  Asynchronousprogramming options and history  Thread and ThreadPool  Asynchronous Programming Model (APM)  Event-based Asynchronous Pattern (EAP)  Task – Basics – Exception handling – Relationships
  • 4.
    What is asynchronous? ImageSource: http://www.webopedia.com/ To do more than one thing at a time. asyn = not with chronos = time
  • 5.
    Asynchronous programming options andhistory  Thread  Asynchronous Programming Model (APM)  Event-Based Asynchronous Pattern (EAP)  BackgroundWorker  Task  Task Parallel Library - DataFlow  Task Parallel Library – Parallel APIs  Thread → APM → EAP → TAP
  • 6.
    Thread and ThreadPool  Basicbuilding block for async programming.  Parameterize thread, came with .NET 2.0 Image source:http://johnpaulmorabito.blogspot.in/
  • 7.
    APM and EAP  APMdepends upon Begin and End methods.  Callbacks are dependent on IAsyncResult.  EAP came with .NET 2.0 which automatically handles synchronization context.  BackgroundWorker is an example of EAP based programming.  EAP can be seen while adding web reference which automatically creates with proxies classes.
  • 8.
  • 9.
    Task  A task isan abstraction of unit of work to run asynchronously.  Ways to execute: new Task(Speak).Start(), Task.Factory.StartNew(Speak), Task.Run(Speak)  Use FromAsync when an API has BeginXXX/EndXXX.  Task can be canceled through CancellationToken and checked with ThrowIfCancellationRequested.  IProgress<T> came with 4.5 which can be used to show status
  • 10.
    Task error handling  Taskare completed with following state: Ran to Completion, Canceled and Faulted.  Task uses AggregateException to wrap up any other exception occurs which can be found under inner exception.  AggregateException comes with Flatten method to check all exceptions.  AggregateException also comes with Handle method which accepts predicate to handle errors.  ThrowUnobservedTaskExceptions to configure to shallow exception.
  • 11.
  • 12.
  • 13.
    Task Relationships Task Task TaskTask Child Tasks Task Task Task Chained Task
  • 14.
    Task Relationships...  ContinueWhenXXX canbe used to chain up tasks with TaskContinuationOptions enum for various conditions.  TaskCreationOptions.AttachedToParent is used to create child tasks.  Creating child task will reflect exception in parent task if exception occurs.  Task.Run uses TaskCreationOptions.DenyChildAttach by default which will act as nested task.
  • 15.
  • 16.
    Presenter: XXXX XXXX,Mindfire Solutions Question and Answer
  • 17.
    References  Pro Asynchronous Programmingwith .NET  Asynchronous Programming Patterns  Asynchronous Programming in .NET: I'll Call You Bac (For definition)
  • 18.
    Presenter: Vikash Kumar,Mindfire Solutions Thank you
  • 19.