reactiveui / ReactiveUI

An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms that is inspired by functional reactive programming. ReactiveUI allows you to abstract mutable state away from your user interfaces, express the idea around a feature in one readable place and improve the testability of your application.

Home Page:https://www.reactiveui.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: WPF ReactiveCommand stays disabled after first execution

massimilianokraus-kappas opened this issue ยท comments

Describe the bug ๐Ÿž

After the first execution, the buttons bound to a ReactiveCommand<Unit, Unit> remain disabled forever. It seems that the IsExecuting observable never raises a "false" value.

Step to reproduce

Create a WPF project.

Add the NuGet packages System.Reactive (6.0.0) and ReactiveUI (19.5.1).

Write this content in the XAML Window:

<Button Content="{Binding Content}" Command="{Binding Increment}" />

Write this content in the MainWindow.xaml.cs:

using System.Diagnostics;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Threading.Tasks;
using System.Windows;
using ReactiveUI;

using static System.ObservableExtensions;

namespace ReactiveCommandTest;

public partial class MainWindow : Window
{
    public MainWindow() {
        RxApp.MainThreadScheduler = Scheduler.CurrentThread;
        InitializeComponent();
        DataContext = new MainWindowVm();
    }
}

public class MainWindowVm : ReactiveObject
{
    public MainWindowVm() {
        Content = "Initial value";
        Increment = ReactiveCommand.CreateFromTask(async () => {
            await Task.Delay(1000);
            Content = "Changed";
        });
        Increment.Subscribe(x => Debug.WriteLine("Command executed"));
        Increment.CanExecute.Subscribe(canExecute => Debug.WriteLine("canExecute: " + canExecute));
        Increment.IsExecuting.Subscribe(isExecuting => Debug.WriteLine("isExecuting: " + isExecuting));
    }

    public string Content {
        get { return _Content; }
        set { this.RaiseAndSetIfChanged(ref _Content, value); }
    }
    private string _Content;

    public ReactiveCommand<Unit, Unit> Increment { get; set; }
}

Run the application and click the Button on the UI.
The Text of the Button changes correctly into "Changed".
But the Button stays disabled.

At the Click, the Console prints this:

canExecute: False
isExecuting: True

At the end of the Command, the Console prints this:

Command executed

So the Command is actually finished. Why the IsExecuting is not firing a "false" value?

It does not make any difference to remove the subscription to CanExecute, IsExecuting or to the Command itself, nor passing a canExecute parameter of any kind inside the factory method.

The "RxApp.MainThreadScheduler = Scheduler.CurrentThread;" is necessary of the Click causes the Exception:

"The calling thread cannot access this object because a different thread owns it."

Reproduction repository

https://github.com/reactiveui/ReactiveUI

Expected behavior

After 1 second, the command finishes its execution, therefore the Button should return to an enabled state and I should be able to click it again to execute the command again.

Screenshots ๐Ÿ–ผ๏ธ

No response

IDE

No response

Operating system

Windows

Version

10

Device

Windows virtual machine

ReactiveUI Version

19.5.1

Additional information โ„น๏ธ

No response

Hi, you seem to be attempting to set the property of a UI element inside a task, the task is most likely crashing causing the Command to post to it's Error subscription, therefore all other subscriptions will not complete.
By setting RxApp.MainThreadScheduler = Scheduler.CurrentThread; you are setting the UI thread to what could be a random thread as it will be whatever thread is executing the constructor, it's possible that this is not the correct UI thread.
Setting of UI components should be done through properties in the ViewModel and bindings in the View, I believe that your doing this, however the binding uses the RxApp.MainThreadScheduler thread and you have manipulated this to be a random thread instead of the Pre defined UI thread for WPF. This will cause the Binding to fail also therefore the UI status will not be updated as the underlying function has crashed.
Currently ReactiveCommand.CreateFromTask does not guarantee that errors will be posted when they occur, when they do it's quite random as to whether or not the Task will return any result, hence the issue occurs, making sure you put error handlers inside of the execution code will help to ensure errors are handled better.
Subscribing to the Errors of each ReactiveCommand will help you to see why the command may not be returning the completed status.
Hopefully this helps you to understand some of the issues that are occurring in your code.

Thank you for the quick replay.
You say this RxApp.MainThreadScheduler = Scheduler.CurrentThread could be any Thread, but with this the code works (the command sets the VM values correctly and therefore the UI, apart from the enabled); without it, the app breaks immediately as soon as I click the button, with the cross-Thread Exception I described, without even touching the UI or setting any value.

What is the correct way to set RxApp.MainThreadScheduler? In the App.xaml.cs? Somewhere else?

The default value is an instance of System.Reactive.Concurrency.DefaultScheduler, which is not working.

Also, if I add this line:

Increment.ThrownExceptions.Subscribe(x => Debug.WriteLine($"ThrownExceptions? {x}"));

Nothing happens, the Observable never notifies exceptions.

   Increment = ReactiveCommand.CreateFromTask(async () => {
            await Task.Delay(1000);
            Content = "Changed";
        });

The Content = "Changed" would be happening on a non-UI thread. The subscribe is run on the UI thread but the Task part is not deliberately.

You can feed results from your CreateFromTask to your main UI thread.

See the docs how.

"The Content = "Changed" would be happening on a non-UI thread.".
This does not matter. The WPF binding is thread-safe: you can change a property from another thread, the UI update is always triggered on the Main Thread.

Actually the solution was to switch package: from ReactiveUI to ReactiveUI.WPF.
There is a warning telling that that library is for .NET 4.6.1 (or similar), but it works as a charm.
No need to set RxApp.MainThreadScheduler, and the Buttons return active after the execution.

it's not, only if you're using wpf binding is it thread safe. If you're setting it direct it won't be.

"The Content = "Changed" would be happening on a non-UI thread.". This does not matter. The WPF binding is thread-safe: you can change a property from another thread, the UI update is always triggered on the Main Thread.

Actually the solution was to switch package: from ReactiveUI to ReactiveUI.WPF. There is a warning telling that that library is for .NET 4.6.1 (or similar), but it works as a charm. No need to set RxApp.MainThreadScheduler, and the Buttons return active after the execution.

You need to set your target to include windows10.0.19041.0 for example net7.0-windows10.0.19041.0 then you will be using the correct ReactiveUI and related libraries.
Sorry I didn't realise you were not using the Wpf package of ReactiveUI with Wpf.
We aim to make the framework ready to use without much configuration for each of the packages available.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.