none
WP7 How To quickstarts

    General discussion

  • I was reviewing the How To guides for using Rx for Phone 7 development. The guides are quite well written. Check them out at

    http://msdn.microsoft.com/en-us/library/ff637517(v=VS.92).aspx and
    http://msdn.microsoft.com/en-us/library/ff637521(v=VS.92).aspx

    I did notice that the emulators for the Accelerometer and Location services both expose the IEnumerable interface rather than IObservable. Here's the example for the Accelerometer emulator:

    static IEnumerable<Vector3> EmulateAccelerometerReading()
    {
        // Create a random number generator
        Random random = new Random();

        // Loop indefinitely
        for (double theta = 0; ; theta+=.1 )
        {
            // Generate a Vector3 in which the values of each axes slowly drift between -1 and 1 and
            // then normalize the vector
            Vector3 reading = new Vector3((float)Math.Sin(theta), (float)Math.Cos(theta * 1.1), (float)Math.Sin(theta * .7));
            reading.Normalize();
                   
            // At random intervals, generate a random spike in the data
            if (random.NextDouble() > .95)
            {
                reading = new Vector3((float)(random.NextDouble() * 3.0 - 1.5),
                 (float)(random.NextDouble() * 3.0 - 1.5),
                 (float)(random.NextDouble() * 3.0 - 1.5));

            }

            // return the vector and then sleep
            yield return reading;
            Thread.Sleep(100);
        }
    }

    Besides the fact that the thread.sleep is never called due to the yield preceeding it, it seems that using IEnumerable and forcing the call to AsObservable is a bit of a waste of an opportunity. As an alternative, I would think the following would be better for this example:

     public static IObservable<Vector3> GetEmulator()
            {
                var obs = Observable.Create<Vector3>(subscriber =>
                    {
                        Random rnd = new Random();
                        for (double theta = 0; ; theta += .1)
                        {
                            Vector3 reading = new Vector3(
                                (float)Math.Sin(theta),
                                (float)Math.Cos(theta * 1.1),
                                (float)Math.Sin(theta * .7));
                            reading.Normalize();

                            if (rnd.NextDouble() > .95)
                            {
                                reading = new Vector3(
                                    (float)(rnd.NextDouble() * 3.0 - 1.5),
                                    (float)(rnd.NextDouble() * 3.0 - 1.5),
                                    (float)(rnd.NextDouble() * 3.0 - 1.5));
                            }
                            // return the vector and sleep before repeating
                            Thread.Sleep(100);
                            subscriber.OnNext(reading);
                        }
                    });
                return obs;
            }

    In addition, instead of explicitly starting a new thread in the button click to start the emulator, would it be better to just call directly to the StartAccelerometer directly with the following implementation:

            IDisposable subscribed;
            private void StartAccelerometerEmulation()
            {
                if (subscribed == null)
                {
                    var emulationObservable = GetEmulator();
                    subscribed = emulationObservable.SubscribeOn(Scheduler.ThreadPool).Subscribe(AccelerometerReadingEventArgs => InvokeAccelerometerReadingChanged(AccelerometerReadingEventArgs));
                }
                else
                {
                    subscribed.Dispose();
                    subscribed = null;
                }
            }

    (I've included the subscribed field here to allow for starting and stopping the emulator on button click.

    As I said, I think these how to guides are very well written. The above tweaks could help to make them even more reactive.

    Jim


    http://www.LinqInAction.net - "LINQ In Action", The book is now available. Don't wait for the movie
    Wednesday, July 21, 2010 12:46 AM

All replies