C# 如何使用System.Linq.Observable.Throttle()
下面的代码使用System.Reactive中的Observable类。我正在使用2009年11月的Silverlight 3工具包C# 如何使用System.Linq.Observable.Throttle(),c#,linq,system.reactive,C#,Linq,System.reactive,下面的代码使用System.Reactive中的Observable类。我正在使用2009年11月的Silverlight 3工具包 private IObservable<Event<EventArgs>> _ob; private IDisposable _proxy; ... private void Init() { _ob = Observable .FromEvent<EventArgs>( x_Grid, "Lay
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( () => { } );
}
private IObservable\u ob;
私有IDisposable_代理;
...
私有void Init()
{
_ob=可观察
.FromEvent(x_网格,“布局更新”)
.节流阀(2000年);//***{};
}
代码段导致异常:
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)
System.ObjectDisposedException:无法访问已处置的对象。
at System.Threading.TimerBase.ChangeTimer(UInt32 dueTime,UInt32时段)
at System.Threading.Timer.Change(Int32 dueTime,Int32 period)
在System.Linq.Observable.c_uuuDisplayClass175`2.c_uuDisplayClass17A.b_uu173(对象)
在System.Threading.\u TimerCallback.TimerCallback\u上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)
在System.Threading.\u TimerCallback.PerformTimerCallback(对象状态)
如果Throttle()方法不存在,则代码工作正常。异常堆栈跟踪指示访问已释放的对象。我所知道的唯一一次性对象是Subscribe()调用返回的对象:但它尚未被处理
有人能指出这段代码的问题吗?是否在InitializeComponent之前(即在正确加载Xaml之前)调用Init()。否则,您的代码看起来是正确的-如果这不能解决问题,我建议您应该升级到SL4(至少尝试Rx)
另外,在SilverLight中Rx的一个更实际的用途是通过(完全公开:我是作者) 这个代码在我看来是正确的。什么是
x_网格
?DataGridView
?x_网格是一个System.Windows.Controls.Grid,即silverlight数据网格。事件名称或事件arg类型没有问题,因为代码在没有Throttle()的情况下运行良好。自去年以来,Rx中出现了许多更改和错误修复。如果可能的话,用最新的Rx版本在SL3上试一试。我在我原来的帖子中打了个错字。我正在使用SL3 2009年11月工具包。不过,我要试试最新的一滴。