Reactive ControlScheduler Extensions

Well, I use the ReactiveExtensions event handler to handle the events of my application, I also use the ControlScheduler to run the handler in the ui thread. However, lately I am getting Cross Thread exception despite using ControlScheduler and I don't know what the problem is

the code:

Observable.FromEventPattern<string>(cc, "UiAlertMessage", new ControlScheduler(this)).Subscribe(_ =>
{
    AlertControl.Show(this, Language.Title, _.EventArgs.UppercaseFirst());
});

Is it supposed to new ControlScheduler(this)run code in the user interface thread, so I am not getting Cross Threading exception?

+3
source share
1 answer

You have to do

Observable.FromEventPattern<string>(cc, "UiAlertMessage")
    .ObserveOn(this)
    .Subscribe(_ =>
    {
        AlertControl.Show(this, Language.Title, _.EventArgs.UppercaseFirst());
    });

, . , subscribes , observes. . ObserverOn SubscribeOn

, ObserveOn System..Reactive.Windows.Forms.

public static IObservable<TSource> ObserveOn<TSource>
(this IObservable<TSource> source, Control control)
{
  if (source == null)
    throw new ArgumentNullException("source");
  if (control == null)
    throw new ArgumentNullException("control");
  else
    return Synchronization.ObserveOn<TSource>(source, (IScheduler) new ControlScheduler(control));
}
+4

All Articles