Ever looking for a new, generic, way to incorporate asynchronous programming capabilities into your applications? Ever wish you could just write a LINQ query over your application events and just create reaction methods for when an event occurs?

Well now you can with the .NET Rx Framework - which provides a new look at asynchronous programming.  It’s really just LINQ over IEnumerable…

Here’s a good way on how to think about this new capability – start with the fact that there are really only (2) types of programs out there:

  • Interactive – your program is running and always in control – most common method of programming.
  • Reactive – the environment sends stuff asynchronously to your program and you can receive details about when that happens. 

Today,  reactive programming by specified by methods that are called at unpredictable times – like:

button.MouseMove += (o, mouseEventArgs) => Debug.Writeline(“You moved the mouse to {0}”, mouseEventArgs.GetPosition(button));

Compare this with an .NET Rx query that creates a dragging event for a Silverlight/WPF control:

IObservable<Event<MouseEventArgs>> draggingEvent =
     from mouseLeftDownEvent in control.GetMouseLeftDown()
     from mouseMoveEvent in control.GetMouseMove().Until(control.GetMouseLeftUp())
     select mouseMoveEvent;

A verbal description of this query might be:

“For each mouse left down event, get each mouse move event and return it until the next mouse left up event occurs.”

As you can see, using from” allows us to declaratively sequence events.  The data passed to event handlers and callbacks can be thought of as sequences of data that are “pushed” at you rather than “pulled.”… 

Every time an event is fired we get “pushed” a new piece of data: the EventArgs.  Similarly when a callback is invoked it is typically “pushed” the result of the asynchronous method. 

The Silverlight Toolkit team is using Rx to write reliable, event-based asynchronous tests.  As of today the only place you can get it is in the Silverlight Toolkit

Check out these blog posts for additional information:

Technorati Tags: ,

kick it on Shout it