【问题标题】:Why task is not running even when execution is inside the task method?Why task is not running even when execution is inside the task method?
【发布时间】: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


    【解决方案1】:
    猜你喜欢
    • 2022-12-27
    • 1970-01-01
    • 2022-12-02
    • 2020-10-24
    • 2022-12-27
    • 2022-12-27
    • 2021-06-16
    • 2022-11-20
    相关资源
    最近更新 更多