Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncRelayCommand IsRunning only true on return of Task #1053

Open
1 of 4 tasks
labsin opened this issue Feb 25, 2025 · 0 comments
Open
1 of 4 tasks

AsyncRelayCommand IsRunning only true on return of Task #1053

labsin opened this issue Feb 25, 2025 · 0 comments
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior

Comments

@labsin
Copy link

labsin commented Feb 25, 2025

Describe the bug

The ExecutionTask and IsRunning of the AsyncRelayCommand is only set after the execute returns a Task, so on the first real async function.
As long as it's running synchronously, the ExecutionTask is still the previous task and IsRunning is based on that one

Regression

No response

Steps to reproduce

  1. Create an AsyncRelayCommand with an async function that returns a Task
  relayCommand = AsyncRelayCommand(Execute);
  1. Check if is IsRunning in the synchronous part of the execution method
  async Task Execute()
  {
     Debug.Assert(relayCommand.IsRunnung); // Fails
     Debug.Assert(relayCommand.AsyncRelayCommand == null); // at least not be the previous task?
     await Task.Wait(1000);
  }

Expected behavior

That AsyncRelayCommand is set to null and IsRunning is true before the execute method is called.

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.2.0

Additional context

Ideally, you'd not call the IsRunning inside the Execute call, but I've done it by accident already that I run setup that I wanted to skip when a command is running, that is called from the Execute function before any network calls.

Workaround is just doing

await Task.Yield();

as the first line in the Execute method.

Help us help you

Yes, I'd like to be assigned to work on this item

@labsin labsin added the bug 🐛 An unexpected issue that highlights incorrect behavior label Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior
Projects
None yet
Development

No branches or pull requests

1 participant