Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Crash when calling ChangeCanExecute() from non-UI thread #5679

Open
Mikilll94 opened this issue Mar 25, 2019 · 2 comments
Open

Crash when calling ChangeCanExecute() from non-UI thread #5679

Mikilll94 opened this issue Mar 25, 2019 · 2 comments
Labels
e/5 🕔 5 help wanted We welcome community contributions to any issue, but these might be a good place to start! i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often inactive Issue is older than 6 months and needs to be retested t/bug 🐛 up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!

Comments

@Mikilll94
Copy link

Description

App crashes when running async method which triggers ChangeCanExecute(). I think this should the responsibility of the renderers to trigger any visual updates on the UI thread. The workaround for this problem is to wrap all ChangeCanExecute() in Device.BeginInvokeOnMainThread()

Steps to Reproduce

Run async methods which triggers ChangeCanExecute(). You can i.e. create a button with async Command which changes one of the binding property and this binding property raise ChangeCanExecute() in the setter.

Expected Behavior

App should not crash.

Actual Behavior

App crashes when running async method which triggers ChangeCanExecute(). The problem exists both on Android and iOS.

Basic Information

  • Version with issue: Xamarin Forms 3.6.0.264807
  • Last known good version:
  • IDE: Visual Studio 2017 15.9.4
  • Platform Target Frameworks:
    • iOS: 8.0
    • Android: 8.1

Reproduction Project

Here is a project with a repro:

App1.zip

@kingces95
Copy link
Contributor

image

Well, throws something but my debugger doesn't quite let me dig the message out...

@Mikilll94
Copy link
Author

@kingces95

You have to click "Press to see the error" button. This will fire "AnotherCommand" in the view model. In view model I have wrapped the command in try catch block. Here is the code:

        private Task AnotherCommandExecute()
        {
            return Task.Run(() =>
            {
                try
                {
                    ButtonActivated = !ButtonActivated;
                }
                catch(Exception e)
                {
                    Debug.WriteLine(e);

                    throw;
                }
            });
        }

As you can see I caught the exception and wrote it to the output using Debug.WriteLine. If you want you can set a breakpoint on Debug.WriteLine and look and the "e" variable. The error message says that you can only make a change in UI on the main thread. Thus ChangeCanExecute() cannot be executed by an async method because the will crash the app. And I think that should not work like that. Renderers should be responsible for executing UI changes on the main thread.

@samhouts samhouts added the s/unverified New report that has yet to be verified label Apr 9, 2019
@samhouts samhouts added e/5 🕔 5 and removed s/unverified New report that has yet to be verified labels May 11, 2019
@samhouts samhouts added the i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often label May 29, 2019
@samhouts samhouts added inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! labels Sep 26, 2019
@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@samhouts samhouts removed this from the 5.0.0 milestone Nov 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
e/5 🕔 5 help wanted We welcome community contributions to any issue, but these might be a good place to start! i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often inactive Issue is older than 6 months and needs to be retested t/bug 🐛 up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!
Projects
None yet
Development

No branches or pull requests

3 participants