Asynchronous Simplified


Introduction

For recent developments, users want reactive graphical interfaces, even if you need to recover data from external sources (ex. with web services).
The best solution is to create asynchronous operations to let a responsive UI.
Since the Framework 4.5, Microsoft offers a new and easy way to use asynchronous methods.

Basically, you can create a software with two methods:

  • void LongMethod() to run something that takes a lot of time (ex. 2 seconds).
  • string QuickMethod() to run something that takes little time (ex. 1 second).

public void LongMethod()
{
    System.Threading.Thread.Sleep(2000);
}
public string QuickMethod()
{
    System.Threading.Thread.Sleep(1000);
    return "ABC";
} 

1. Synchronous methods

when you start successively these two methods, of course the User Interface freezes until the procedure is completed.

MyLongMethod();
MyQuickMethod();

And the execution traces will be… and my UI not responding.

 

2. Classic Asynchronous methods

To avoid the UI freezes, you can run these methods into two Threads, via Task.Factory.StartNew or simply Task.Run (see difference on PFXTeaml’s Blog).

Task.Run( () => MyLongMethod() );
Task.Run( () => MyQuickMethod() );

And the execution traces will be… My UI is responding, but of course, you can see a problem: the Main Finished come before each method Finished steps.

3. async / await methods

The best solution, is to use the new async / await keywords. In this case, my methods will be run in different threads (versus my UI) and they wait each finish to continue my main program. And note that, by convention, a method called asynchronously will be suffixed by the Async keyword.

async void Go()
{
   await MyLongMethodAsync();
   await MyQuickMethodAsync();
}

And the execution traces will be… My UI is responding, and the program workflow is correct.

Quick Tips

Unfortunately, if you try to update a UI component or a Collection binded to your UI, from a thread, you’ll receive a NotSupportedException.

You can solve that with two different methods (for a WPF application):

  • The full parameterized method, where you can define a priority level and a timeout beyond which the process will be canceled.

Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, 
                                      TimeSpan.FromSeconds(1), 
                                      new Action( () =>
   {
      // Method to execute in the UI Thread.
      this.ConsoleItems.Add(new ConsoleItem(text, level));
   }
));

  • The light method: before run your asynchronous methods, keep a reference of UI (SynchronizationContext.Current), and next call the Send method.

public SynchronizationContext UIContext { get; set; }

UIContext.Send((state) => 
{
   // Method to execute in the UI Thread.
   this.ConsoleItems.Add(new ConsoleItem(text, level));
}, 
null);
 

 

Finally, if sometime you need to run some methods in a Thread different of UI Thread, but you must ensure that all of these methods will be run in the same Thread (only one). So, download the StaTaskScheduler class wrote by Microsoft and available in a ParallelExtensionsExtras package and write your async method like that:

StaTaskScheduler _sta = new StaTaskScheduler(1); // 1 is the NumberOfThreads

Task<TResult> RunTask<TResult>(Func<TResult> function)
{
   return Task.Factory.StartNew<TResult>(function, CancellationToken.None, TaskCreationOptions.None, _sta);
}

Advertisements
Posted in Formation, Thread, Tips and others

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow me
   RSS Feed    Twitter    Linked In

Write you email address to subscribe at this blog and to receive new posts by mail.

Join 565 other followers

Who I am?






%d bloggers like this: