Git Product home page Git Product logo

Comments (9)

ChrisPulman avatar ChrisPulman commented on April 28, 2024

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.

from reactiveui.

massimilianokraus-kappas avatar massimilianokraus-kappas commented on April 28, 2024

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.

from reactiveui.

massimilianokraus-kappas avatar massimilianokraus-kappas commented on April 28, 2024

Also, if I add this line:

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

Nothing happens, the Observable never notifies exceptions.

from reactiveui.

glennawatson avatar glennawatson commented on April 28, 2024
   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.

from reactiveui.

massimilianokraus-kappas avatar massimilianokraus-kappas commented on April 28, 2024

"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.

from reactiveui.

glennawatson avatar glennawatson commented on April 28, 2024

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

from reactiveui.

glennawatson avatar glennawatson commented on April 28, 2024

dotnet/wpf#168

from reactiveui.

ChrisPulman avatar ChrisPulman commented on April 28, 2024

"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.

from reactiveui.

github-actions avatar github-actions commented on April 28, 2024

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.

from reactiveui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.