Download async-wp7_source - 850.16 KB
Introduction
Today, line of Internet and multimedia-enabled smartphones have become popular. Fortunately if you are a .NET developer you can use your existing skills and language and target some of the most popular mobile operating systems.
Windows Phone 7 is a mobile operating system developed by Microsoft, and is the successor to their Windows Mobile platform.
Background
You can expose asynchronous features to client code using one of the following .NET Framework's Asynchronous Programming Models:
- IAsyncResult The CLR’s Asynchronous Programming Model (APM)
- Event-based Asynchronous Pattern (EAP)
IAsyncResult has been around since .NET 1.0 and has been used in most BCL classes, while the main benefit of the EAP is that it integrates with the Visual Studio UI designers. You can learn how to properly implement the IAsyncResult interface (APM) from the excellent article on MSDN, Implementing the CLR Asynchronous Programming Model, by Jeffrey Richter.
In this post I will show you how easy it is to use types implementing the IAsyncResult interface on Windows Phone 7. I will use the PowerThreading library[1] because it provides a similar (not to say an improved) implementation of the one described in the original MSDN article. I will explain how to use it and how this becomes easier using the AsyncEnumerator class.
The Problem
The code above is used for the demo. This code does some I/O which causes a thread to block.
Sync I/O pauses the UI
The code below shows how ExecuteWithSyncIO method is implemented. The application shows a MessageBox to the user that the UI will pause while the execution is in progress.
Delegate's BeginInvoke method is not supported
The code below shows how ExecuteWithDelegateBeginInvoke method is implemented.
This method is here just for the demo, since it is not allowed to invoke a delegate asynchronously in the Compact Framework.
The Solution
The Wintellect.Threading.AsyncProgModel.AsyncResult<TResult> class contains an implementation of the IAsyncResult interface. This type is generic and we can easily use it on the WebService class.
Actually the GetStockQuotesHelper method could be inlined. I try to avoid inlined delegates because you can easily access variables defined in the parent method body.
Let's look now how the above can be used on Windows Phone 7.
IAsyncResult interface
The code below shows how ExecuteWithIAsyncResult method is implemented. The only problem is that, when using the IAsyncResult, you need to specify a method to be called when a corresponding asynchronous operation completes. This can result in using synchronization constructs to avoid race conditions. It also splits the flow of your code. You can inline the callback method using Anonymous Methods or Lamda Expressions as shown below but if your logic is complicated your code will not be beautiful.
AsyncEnumerator class
The code below shows how ExecuteWithAsyncEnumerator method is implemented. As you can see this method makes your code looks like it's executing synchronously but actually it executes asynchronously. You do not have to split your code in callback methods or inlined delegates. You do not need to marshall calls in the UI thread using the Dispacher or the SynchronizationContext. All this stuff is handled by the AsyncEnumerator class.
Points of Interest
While what I've discussed in this post applies to Mobile application development, the same principles can be applied to Rich Internet applications andSmart client. I have been using the AsyncEnumerator class for over two years and I have to say that it changed the way I think about using the APM. At the end.. delivering responsive applications makes theend-users happy.
More samples can be found on my GitHub repository.
References
[1] AsyncEnumerator class resides in the PowerThreading library. It is written by Jeffrey Richter and can be obtained from the Wintellect website.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)