2010-08-31 20:25
With the many posts that Iв ve done on the Reactive Extensions for both JavaScript and .NET, Iв ve covered a wide variety of the basics as well as some of the deeper stuff. 160; This time, Iв d like to get back to the basics that I do when I give a talk on Rx in person, and this time explaining some of the problems we face today with reactive programming in general, asynchronous and event-based. Why Do We Need It Letв s go into an example of using the event-based asynchronous programming methods that became a bit more prevalent when .NET 2.0 came around, especially with the introduction of WCF in the .NET 3.0 timeframe. 160; This was an alternative to the other methods of asynchronous programming which included the Begin/End Pattern and using WaitHandles. 160; One distinct advantage it did have was for the ability to report progress, report incremental results or state changes that the others could not provide. 160; But with it, comes many downsides as well which weв ll go into and where the Reactive Extensions can help. 160; Typically, weв d have some form of asynchronous method which has no return value and takes in any number of parameters, and a given user token which would allow us to correlate our token with what comes in the user state to make sure weв re looking at the right result. 160; public void DoSomethingAsync(object token) // Kick off something async And weв d also have some form of EventArgs which would encompass our result and exception should one occur and optionally a cancelled flag should we need to do so and be notified that it happened. 160; Then weв d also have our EventHandler which is then called when our asynchronous method is complete. public class DoSomethingCompletedEventArgs : EventArgs public string Result get; set; public bool Cancelled get; set; public Exception Error get; set; public event EventHandler lt;DoSomethingCompletedEventArgs gt; DoSomethingCompleted; To give you an idea of some of the problems we face with this pattern, letв s go over an example using the System.Net.WebClient class and downloading a string asynchronously via the DownloadStringAsync method. 160; Letв s enumerate some of the challenges in the code below. var uri = new Uri( quot;http://search.twitter.com/search.json q=4sq.com quot;); var client = new WebClient(); client.DownloadStringCompleted += (sender, args) // TODO: Get the only one we want // TODO: Check for cancel // TODO: Check for exception // TODO: Check for result // TODO: What about more composition ; client.DownloadStringAsync(uri); // TODO: How to unsubscribe client.DownloadStringCompleted -= The first challenge is how to make sure we get the value we want. 160; After all, if multiple calls are made to this WebClient instance, weв ll need to correlate the user state with our token. 160; Next, we need to check for cancellation and do something when that happens. 160; After checking for cancellation, we might also want to check for exceptions and handle them appropriately as...
Read More...
| © | 2012 |