本文介绍了Silverlight 4中的节流功能错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

 

我使用的是Silverlight 4.0和Reactive Extention 1.0

I am using Silverlight 4.0 and Reactive Extention 1.0

以下代码对我来说工作正常

The following code is working fine for me

IObservable< EventPattern< RoutedEventArgs>> chkInWorkflow = Observable.FromEventPattern< RoutedEventArgs>(txtMain," TextChanged");

IObservable<EventPattern<RoutedEventArgs>> chkInWorkflow = Observable.FromEventPattern<RoutedEventArgs>(txtMain, "TextChanged");

var input =(from ch in chkInWorkflow select((TextBox)e.Sender).Text);

var input = (from e in chkInWorkflow select ((TextBox)e.Sender).Text);

input.Subscribe(A => tbMessage.Text + =" |" + A);

input.Subscribe(A => tbMessage.Text += "|" + A);

 

但是如果我试图使用Throttle Function它会给出一个错误

But if i am trying to use Throttle Function it give an Error

IObservable< EventPattern< RoutedEventArgs>> chkInWorkflow = Observable.FromEventPattern< RoutedEventArgs>(txtMain," TextChanged");

IObservable<EventPattern<RoutedEventArgs>> chkInWorkflow = Observable.FromEventPattern<RoutedEventArgs>(txtMain, "TextChanged");

var input =(from ch in chkInWorkflow select((TextBox)e.Sender).Text) .DistinctUntilChanged()。节流(TimeSpan.FromSeconds(0.3));

var input = (from e in chkInWorkflow select ((TextBox)e.Sender).Text).DistinctUntilChanged().Throttle(TimeSpan.FromSeconds(0.3));

input.Subscribe(A => tbMessage.Text + =" |" + A);

input.Subscribe(A => tbMessage.Text += "|" + A);

 

On TextChanged发生以下错误的文本框事件:

On TextChanged Event of Textbox following error occured :

未处理的异常('Silverlight应用程序中的未处理错误')

代码:4004

类别:ManagedRuntimeError

消息:System.UnautorizedAccessException:无效的跨线程访问。

An unhandled exception ('Unhandled Error in Silverlight Application')
Code : 4004
Category : ManagedRuntimeError
Message : System.UnautorizedAccessException : Invalid Cross-thread access.

 

谢谢,

Gurpreet

推荐答案

这是因为Throttle扩展方法引入了并发性,您的订阅操作正在调度程序以外的其他线程上运行。  实际上,Throttle默认使用Threadpool。由于Silverlight和WPF要求UI上的所有
操作都发生在调度程序上,因此您将不得不将其编组回该线程。

This is because the Throttle extension method introduces concurrency and your subscription action is being run on another thread other than the dispatcher.  In fact, Throttle by default uses the Threadpool. Since Silverlight and WPF require that all actions on the UI occur on the dispatcher, you're going to have to marshal it back onto that thread.

要解决此问题,请执行此操作这个:

To solve this, do this:

input
  .ObserveOn(Scheduler.Dispatcher)
  .Subscribe(A => tbMessage.Text += "|" + A);

这将导致调度程序上发生OnNext,OnComplete和OnError操作。

This will cause the OnNext, OnComplete and OnError actions to occur on the dispatcher.

如果您愿意对Throttle如何工作的细粒度控制,你会注意到IScheduler实例有不同的重载。

If you'd like finer grained control over how Throttle works, you will notice that there are different overloads that take an IScheduler instance.

 


这篇关于Silverlight 4中的节流功能错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 04:38