CommunityToolkit / CommunityToolkit/dotnet
AsyncRelayCommand IsRunning only true on return of Task
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### 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);
```
2. 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
Contributor guide
Assessment
This issue has not been assessed yet.