dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

AsyncRelayCommand IsRunning only true on return of Task

Open labsin opened this issue 1 year ago • 0 comments

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
  • [x] 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 avatar Feb 25 '25 13:02 labsin