views:

142

answers:

1

I have the following code that uses the Observable class from System.Reactive. I'm using the November 2009 Silverlight 3 toolkit.

private IObservable<Event<EventArgs>> _ob;
private IDisposable _proxy;
 ... 

private void Init()
{
  _ob = Observable
           .FromEvent<EventArgs>( x_Grid, "LayoutUpdated" )
           .Throttle( 2000 );  // *** <- The problem
  _proxy = _ob.Subscribe( () => { } );
}

The code snippet results in the exception:

System.ObjectDisposedException: Cannot access a disposed object.
   at System.Threading.TimerBase.ChangeTimer(UInt32 dueTime, UInt32 period)
   at System.Threading.Timer.Change(Int32 dueTime, Int32 period)
   at System.Linq.Observable.<>c__DisplayClass175`2.<>c__DisplayClass17a.<Generate>b__173(Object _)
   at System.Threading._TimerCallback.TimerCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading._TimerCallback.PerformTimerCallback(Object state)

If the Throttle() method is not present, the code works fine. The exception stack trace indicates accessing a disposed object. The only disposable object I am aware of is the one returned by the Subscribe() call: but this has not been disposed.

Can someone point to the problem with this code.

A: 

Is Init() being called before InitializeComponent (i.e. before the Xaml is loaded properly?) Your code looks correct otherwise - if that doesn't fix it, I'd say you should upgrade to SL4 (at least to try out Rx).

Also, a more practical use of Rx in SilverLight is via the ReactiveXaml library (full disclosure: I am the author).

Paul Betts