How to get a WinForm synchronization context or schedule on a WinForm thread
Asked Answered
K

1

13

I have a winform application, and an observable set up like this:

Form form = new Form();
Label lb = new Label();
form.Controls.Add(lb);

Observable.Interval(TimeSpan.FromSeconds(1))
          .Subscribe(l => lb.Text = l.ToString());

Application.Run(form);

This doesn't work, since the l => lb.Text = l.ToString() will not be run on the main thread which created the form, but I cannot figure out how to make it run on this thread. I assume, that I should use IObservable.SubscribeOn which takes either an IScheduler or a SynchronizationContext, but I don't know how to get the synchronizationcontext of the main thread, and the only Schedulers I could find were the static properties of Scheduler, such as Scheduler.CurrentThread, Immediate, NewThread, TaskPool and ThreadPool, none of which worked.

My Rx version is 1.0.10621.

Kinesics answered 14/9, 2011 at 14:25 Comment(0)
K
26

Just after I post the question, I find the solution:

Form form = new Form();
Label lb = new Label();
form.Controls.Add(lb);

Observable.Interval(TimeSpan.FromSeconds(2))
          .ObserveOn(SynchronizationContext.Current)
          .Subscribe(l => lb.Text = l.ToString());

Application.Run(form);

This link was helpful. Two notes:

  • Not all threads have a synchronization context, but the first form that gets created on a thread will set a synchronization context for that thread, so the UI thread always has one.
  • The correct method is ObserveOn, not SubscribeOn. At the moment, I don't know enough about this to know why, so any links in the comments would be appreciated.

edit: Thanks to the first part of this link, I now understand more about the difference between ObserveOn and SubscribeOn. In short:

  • An observable, which observes on a synchronization context will call the IObserver methods (OnNext and friends) from that context. In my example, I observe on the main/UI thread, so therefore I get no cross thread exceptions
  • SubscribeOn is a little more complicated, so here is an example: Concat takes a number of observables and combines them to one long observable. Once an observable calls OnCompleted, the combined observable will dispose of that subscription, and subscribe to the next observable. This all happens on the thread that called OnCompleted, but there can be some problems with subscribing to observables, that were created by Observable.FromEvent, e.g. Silverlight will throw if you add an event handler from another thread than the UI thread, and WinForms and WPF will throw if you add a event handlers from multiple different threads. SubscribeOn will let you control the thread on which your observable hooks up to the underlying event.
Kinesics answered 14/9, 2011 at 14:36 Comment(6)
SubscribeOn only sets which thread the actual subscribing happens on, whereas ObserveOn determines which thread the OnNext calls get executed. For comparison with events, SubscribeOn is like making you only add event handlers on the main thread, but ObserveOn makes sure the event handling routine gets called on the right thread.Rosenda
@Gideon: Thanks. I edited my answer to reflect my improved understanding, but your comments sums it all up very well.Kinesics
If you have a reference to System.Reactive.Windows.Forms.dll you can do .ObserveOn(form) and it will do the equivalent of Control.Invoke.Mixedup
@Anderson: Ah, I had seen code that used the form as an argument - except I remembered it as SubscribeOn instead of ObserveOn - but since I couldn't find that overload, and most of the tutorials I had seen were 1 or 2 years old, I had thought that that part of the API had been deprecated, and was no longer available. Thanks, this makes it even nicer.Kinesics
It works for winform. But just curious, does SynchronizationContext.Current necessarily mean UI thread?Tophus
@Tophus If you call SynchronizationContext.Current from the main thread of a UI application, it will be the main UI thread. Otherwise, it will be the synchronization context of the currently running thread (probably just null).Repugnant

© 2022 - 2024 — McMap. All rights reserved.