下面的代码使用了System.Reactive中的Observable类。我使用的是2009年11月的Silverlight3工具包。
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( () => { } );
}代码片段会导致异常:
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)如果没有Throttle()方法,代码就可以正常工作。异常堆栈跟踪指示访问已释放的对象。我所知道的唯一可丢弃的对象是由Subscribe()调用返回的对象:但是这个对象还没有被释放。
有人能指出这段代码的问题所在吗?
发布于 2010-08-24 11:18:17
是否在InitializeComponent之前调用了Init() (即,在正确加载Xaml之前?)否则你的代码看起来是正确的--如果这不能修复它,我会说你应该升级到SL4 (至少是为了试用Rx)。
此外,Rx在SilverLight中的一个更实际的用法是通过ReactiveXaml library (完全公开:我是作者)。
https://stackoverflow.com/questions/3513882
复制相似问题