ReactiveUI concurrency command (WebClient)

I am using the latest preview version of RxUI 8, but I think this will happen in previous versions.

I defined this ReactiveCommand in my WPF application:

GetWebsiteCommand = ReactiveCommand.CreateFromTask(DownloadString);

private async Task<string> DownloadString()
{
    using (var client = new WebClient())
    {
        return await client.DownloadStringTaskAsync("http://www.google.es");
    }
}

When the command is executed, the following exception is thrown:

System.InvalidOperationException 'in System.Reactive.Core.dll: the calling thread cannot access this object because another thread owns it

Why is this happening? I do not create a single new thread!

This is the stack trace:

System.InvalidOperationException 'in System.Reactive.Core.dll: the calling thread cannot access this object because another thread owns it

 at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at System.Windows.Controls.Primitives.ButtonBase.get_Command()
   at System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute()
   at System.Windows.Controls.Primitives.ButtonBase.OnCanExecuteChanged(Object sender, EventArgs e)
   at System.Windows.Input.CanExecuteChangedEventManager.HandlerSink.OnCanExecuteChanged(Object sender, EventArgs e)
   at ReactiveUI.ReactiveCommand.OnCanExecuteChanged()
   at ReactiveUI.ReactiveCommand`2.<.ctor>b__9_5(Boolean _)
   at System.Reactive.AnonymousSafeObserver`1.OnNext(T value)
   at System.Reactive.Linq.ObservableImpl.RefCount`1._.OnNext(TSource value)
   at System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive(Int32 count)
   at System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive()
   at System.Reactive.Subjects.ReplaySubject`1.ReplayBase.OnNext(T value)
   at System.Reactive.Subjects.ReplaySubject`1.OnNext(T value)
   at System.Reactive.Linq.ObservableImpl.AsObservable`1._.OnNext(TSource value)
   at System.Reactive.Linq.ObservableImpl.DistinctUntilChanged`2._.OnNext(TSource value)
   at System.Reactive.Linq.ObservableImpl.CombineLatest`3._.S.OnNext(TSecond value)
   at System.Reactive.Linq.ObservableImpl.RefCount`1._.OnNext(TSource value)
   at System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive(Int32 count)
   at System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive()
   at System.Reactive.Subjects.ReplaySubject`1.ReplayBase.OnNext(T value)
   at System.Reactive.Subjects.ReplaySubject`1.OnNext(T value)
   at System.Reactive.Linq.ObservableImpl.AsObservable`1._.OnNext(TSource value)
   at System.Reactive.Linq.ObservableImpl.DistinctUntilChanged`2._.OnNext(TSource value)
   at System.Reactive.Linq.ObservableImpl.Concat`1._.OnNext(TSource value)
   at System.Reactive.Linq.ObservableImpl.Select`2._.OnNext(TSource value)
   at System.Reactive.SafeObserver`1.OnNext(TSource value)
   at System.Reactive.ScheduledObserver`1.Dispatch(ICancelable cancel)
   at System.Reactive.Concurrency.Scheduler.<>c.<ScheduleLongRunning>b__72_0(Action`1 a, ICancelable c)
   at System.Reactive.Concurrency.DefaultScheduler.LongRunning.<>c__DisplayClass1_0`1.<ScheduleLongRunning>b__0(Object arg)
   at System.Reactive.Concurrency.ConcurrencyAbstractionLayerImpl.<>c__DisplayClass7_0.<StartThread>b__0()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
+4
source share
1 answer

, canExecute . , , , . , canExecute , IsEnabled Button .

, , ObserveOn canExecute.

UPDATE: .

+2

Source: https://habr.com/ru/post/1686917/


All Articles