Monday, September 15, 2014

Work with Async

Asynchronous Programming

If you are working with applications such as windows store and Windows Phone applications you may be found lot of functions that with Async keyword. Why Async?
We need async because when working with applications that interact with network (web, local network) , database, Files and reading hardware in the device are much more slower than the usual execution. Because of it communication bottlenecks are occur.

To avoid those communication bottlenecks async is born.

What Async Does ?

Async allows to run those delayed tasks in the background and meanwhile program can do what ever operations that does not depend on the awaited task.
simply,
"With Async Application can continue with other works that doesn’t depend on blocked resource until the blocked task finished"

Asynchronous programming available with .NET framework 4,5 onwards with Visual studio 2012. If you working on visual Studio 2012 without installing any of other libraries you can work on async

Concepts

Task
Task encapsulate all units of works in the async. Task contains following properties

  • State : Running , Finished , Cancelled
  • Result
  • Thrown Exceptions
It will handle each and every exceptions and it throws all exceptions when task is finished.(await finished)


Important

If you really want to implement async methods and use them remember async and await keywords.

async keyword informs the compiler this method need to be handle separately by itself. Await keyword indicate the suspension of relative running async task.

When working with async,
  • Use Async Suffix  | e.g. DownloadAsync
  • Return Task or Task<T> according to situation
  • void for only event handlers  

Example Wpf Code with simple async operation

public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
      }
      private void btnNormal_Click(object sender, RoutedEventArgs e)
      {
          resultTxt.Text = getString("sample");
          independentWork();
      }
      private async void btnAsync_Click(object sender, RoutedEventArgs e)
      {
          resultTxt.Text = await getStringAsync();
      }
      /// <summary>
      /// Method with waiting in synchronize way
      /// ///
      /// </summary>
      /// <returns></returns>
      private string getString(string name)
      {
          //Task.Delay(2000);
          Thread.Sleep(2000);
          return "My String";
      }
      /// <summary>
      /// same method calling from async
      /// </summary>
      /// <returns></returns>
      private async Task<string> getStringAsync()
      {
          Task<string> asyncTask = Task.Factory.StartNew(() => getString("sample"));
          independentWork();
          return await asyncTask;
      }
      
      /// <summary>
      /// this work is independent from others
      /// </summary>
      private void independentWork()
      {
          resultTxt.Text += "\n independent work is done ";
      }
  }





Download Sample Code Here

0 comments: