【发布时间】:2022-12-02 13:54:37
【问题描述】:
So this is WPF + MVVM + .NET 4.8 + WCT.
I have an AsyncRelayCommand in my VM class defined like this:
private AsyncRelayCommand _StartSyncCommand;
public AsyncRelayCommand StartSyncCommand
{
get
{
_StartSyncCommand ??= new AsyncRelayCommand(Pump, () => !_StartSyncCommand.IsRunning);
return _StartSyncCommand;
}
}
The actual task method contains an async iterator and looks like this:
private async Task Pump(CancellationToken token)
{
OnPropertyChanged(nameof(IsBusy));
try
{
await foreach (var item in applicationService.FetchItems())
{
token.ThrowIfCancellationRequested();
...
}
}
catch(Exception ee)
{
...
}
finally
{
...
}
}
This method raises property change notification on IsBusy property (to show wait cursor in the UI). However when I check the status of StartSyncCommand in the property, it tells me that it is not running.
public bool IsBusy => StartSyncCommand.IsRunning;
I can\'t see why this should be the case. The method is actually running when the property change notification occurs. I can see the method in the call stack.
What am I missing here?
Update
This is getting weirder. StartSyncCommand.ExecutionTask itself is null while I\'m inside the task method:
标签: c# asynchronous mvvm async-await task-parallel-library