Exposing asynchronous features to client code: Windows Phone 7

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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值