Second Stanza

June 19, 2010

Sequential Asynchronous Workflows in Silverlight Using Rx Framework

Filed under: .NET Development, Rx Extensions, Silverlight — Tags: , — dfbaskin @ 4:44 pm

Jeremy Likness has an excellent post called Sequential Asynchronous Workflows in Silverlight using Coroutines. It seems like this example would also work well using the Reactive (Rx) Extensions Framework.

Here’s the source for an Rx handler of the RandomNumberService:


        /// <summary>
        ///     Once we have the references, start the workflow (the Reactive Way!)
        /// </summary>
        public void OnImportsSatisfied()
        {
            int alpha = 0, red = 0, green = 0, blue = 0;

            int[] maxValuesRequired = new int[]{ 128, 255, 255, 255 };
            var colorWorkflow =
                Observable
                    .ToObservable( maxValuesRequired )
                    .SelectMany( m =>
                        Observable
                            .FromAsyncPattern<int, int>(
                                ( maxValue, callback, state ) => {
                                    GetRandomNumberResult asyncResult = new GetRandomNumberResult();
                                    Service.GetRandomNumber( maxValue, v =>
                                    {
                                        asyncResult.SetResult( v );
                                        callback( asyncResult );
                                    } );
                                    return asyncResult;
                                },
                                ( asyncResult ) => {
                                    return (int)asyncResult.AsyncState;
                                }
                            )( m )
                    )
                    .ObserveOnDispatcher();
            
            int colorIndex = 0;
            colorWorkflow
                .Subscribe( result =>
                {
                    switch( colorIndex++ )
                    {
                    case 0:
                        alpha = result + 128;
                        break;
                    case 1:
                        red = result;
                        break;
                    case 2:
                        green = result;
                        break;
                    case 3:
                        blue = result;
                        break;
                    }
                },
                ex =>
                {
                },
                () =>
                {
                    RectangleFill = new SolidColorBrush(
                        new Color
                        {
                            A = (byte)alpha,
                            B = (byte)blue,
                            G = (byte)green,
                            R = (byte)red
                        } );
                } );
        }

I am wrapping the service call so that it follows the asynchronous service pattern, but this is the more common method of acessing services in a Silverlight application anyway.

Updated July 24, 2010:

I corrected the code by moving the setting of the RectangleFill property to the subscription’s completed method rather than in the Finally method. The latter is more designed for cleaning up after the subscription is completed.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: