I’m getting an ‘UnauthorizedAccesExpection – Invalid cross-thread access’ exception when I try to raise a PropertyChanged event from within a subscription to an IObservable collection created through Observable.Interval().
With my limited threading knowledge I’m assuming that the interval is happening on some other thread while the event wants to happen on the UI thread??? An explanation of the problem would be very useful.
The code looks a little like:
var subscriber = Observable.Interval(TimeSpan.FromSeconds(1))
.Subscribe(x =>
{
Prop = x; // setting property raises a PropertyChanged event
});
Any solutions?
Edit:
This code is being executed from a ViewModel not a DependencyObject.
Edit: I was confusing SubscribeOn with ObserveOn. I’ve updated my answer
You can solve your problem by putting your interval on the dispatcher thread:
Alternatively, you could be able to use
ObserveOnDispatcherbut that would involve jumping threads so I’d recommend against it: