Sunday, July 17, 2011

IEnumerable and IObservable

Recently Microsoft released Reactive Extensions (Rx) at msdn data developer center. After watching Bart de Smet’s DevCamp 2010 Keynote (Rx: Curing your asynchronous programming blues), finally I got some time to try out this library. What is Rx anyway? Well, quoted from Rx’s web site, “The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators.”. Basically, this library provides many operators that make it easier to write asynchronous code and responsive application. Error handling and cancellation are big pain to write asynchronous code, even with support of System.Threading.Tasks.Task introduced by .NET 4.0.  Nothing comes for free, it does requires some kind of mind shifting to use Rx, especially when IEnumerable<T> is heavily used in our mind and programming. Bart de Smet’s keynote really helps, so I strongly suggest your watch it before trying out Rx. Today, I’d like to share you one example I wrote when I tried to understand the usage of Rx.

The goal of my tryout is to analyze all links on a web page, and download all images I’m interested on those linked web pages. So both CPU computation and IO operation are involved. It is quite easy to use DownloadString and DownloadFile of WebClient to download html content  and images of those web pages. Naturally IEnumerable<T> is used to define methods (like “static IEnumerable<string> GetImagePaths(string address)”) of retrieving address from given uri, and CPU computation is mixed with IO operation. As you can image, the program is kind of frozen when it is running and many CPU cycles are wasted on waiting IO completion.

        static IEnumerable<string> GetImagePaths(string address)
        {
            WebClient client = new WebClient();
            var uri = new Uri(address);
            var content = client.DownloadString(uri);
            //analyze html content and get path of images
            foreach (var path in GetJpgImagePaths(content))
            {
                yield return path;
            }
        }

Read more: Junfeng Dai's Blog
QR: ienumerable-and-iobservable.aspx

Posted via email from Jasper-Net