ASYNCHRONOUS PROGRAMMING Code Review 4/26/13 – Chester Hartin
Hopes  Identify where you may need asynchronous processing  Implement it with Background workers,Task Parallel Library, or async & await pattern  Learn to refactor synchronous applications into become more responsive
Asynchronous vs. Parallel  Who remembers my talk about boiling eggs using the parallel extensions?  Concurrency / Boiling several eggs simultaneously  By boiling the eggs in different pots, we are boiling them in parallel to each other.  Now if we do something else while they’re boiling, like cleaning up the kitchen, and we go back to them once they’re done… that task is done asynchronously
Asynchronous vs. Parallel  Think of parallelism as something that runs independently at the same time as another task  Think of asynchronous operations as something that will let you do things in the meantime while you wait, but once it’s finished, you receive the results
Asynchronous  It’s common to look at asynchronous processes as non-blocking (ie the system won’t lock up, will continue).  So how do we do this?
Synchronous Design
Asynchronous Design
Asynchronous  There are several methods  Threads &Thread Pooling  Background workers  Task Parallel Library (TPL)  Asynch & await
Example 1 – No threading  lblInfo.Text = ProcessOrder(); private string ProcessOrder() { // really long task that takes for ever }
Example 2 – background worker  //ex2  private void bgw_DoWork(object sender, DoWor kEventArgs e)  { e.Result = ProcessOrder(); }  private void bgw_RunWorkerCompleted(object s ender, RunWorkerCompletedEventArgs e)  { lblInfo.Text = e.Result.ToString() ; }
Example 3 – TPL (Task Parallel Library )  private void ProcessOrderContinueWith() { var task = newTask<string>(ProcessOrder); task.Start(); task.ContinueWith((parentTask) => { this.Invoke(new Action(() => { lblInfo.Text = parentTask.Result;}));}); lblInfo.Text = "Thinking"; }
Example 3 – TPL (cont)  What happens is we’re creating a State machine  This handles everything in the background & keeps track of what task is running & where so it knows where to go back
Example 4 -- Async & Await private async void ProcessOrderAsync() { var processOrderResult = Task<string>.Factory.StartNew(ProcessOrder); lblInfo.Text = await processOrderResult; }
Example 4 -- Async & Await  Still a state machine, but way easier to read & implement!
Refactoring  Using async & await allows you to utilize most of your existing code

Asynchronous programming

  • 1.
    ASYNCHRONOUS PROGRAMMING Code Review4/26/13 – Chester Hartin
  • 2.
    Hopes  Identify whereyou may need asynchronous processing  Implement it with Background workers,Task Parallel Library, or async & await pattern  Learn to refactor synchronous applications into become more responsive
  • 3.
    Asynchronous vs. Parallel Who remembers my talk about boiling eggs using the parallel extensions?  Concurrency / Boiling several eggs simultaneously  By boiling the eggs in different pots, we are boiling them in parallel to each other.  Now if we do something else while they’re boiling, like cleaning up the kitchen, and we go back to them once they’re done… that task is done asynchronously
  • 4.
    Asynchronous vs. Parallel Think of parallelism as something that runs independently at the same time as another task  Think of asynchronous operations as something that will let you do things in the meantime while you wait, but once it’s finished, you receive the results
  • 5.
    Asynchronous  It’s commonto look at asynchronous processes as non-blocking (ie the system won’t lock up, will continue).  So how do we do this?
  • 6.
  • 7.
  • 8.
    Asynchronous  There areseveral methods  Threads &Thread Pooling  Background workers  Task Parallel Library (TPL)  Asynch & await
  • 9.
    Example 1 –No threading  lblInfo.Text = ProcessOrder(); private string ProcessOrder() { // really long task that takes for ever }
  • 10.
    Example 2 –background worker  //ex2  private void bgw_DoWork(object sender, DoWor kEventArgs e)  { e.Result = ProcessOrder(); }  private void bgw_RunWorkerCompleted(object s ender, RunWorkerCompletedEventArgs e)  { lblInfo.Text = e.Result.ToString() ; }
  • 11.
    Example 3 –TPL (Task Parallel Library )  private void ProcessOrderContinueWith() { var task = newTask<string>(ProcessOrder); task.Start(); task.ContinueWith((parentTask) => { this.Invoke(new Action(() => { lblInfo.Text = parentTask.Result;}));}); lblInfo.Text = "Thinking"; }
  • 12.
    Example 3 –TPL (cont)  What happens is we’re creating a State machine  This handles everything in the background & keeps track of what task is running & where so it knows where to go back
  • 13.
    Example 4 --Async & Await private async void ProcessOrderAsync() { var processOrderResult = Task<string>.Factory.StartNew(ProcessOrder); lblInfo.Text = await processOrderResult; }
  • 14.
    Example 4 --Async & Await  Still a state machine, but way easier to read & implement!
  • 15.
    Refactoring  Using async& await allows you to utilize most of your existing code

Editor's Notes

  • #12 Using a background worker certainly works, but the code will easily get messy and it is not really a linear way of writing code.