I have read: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html and the accepted answer at deadlock even after using ConfigureAwait(false) in Asp.Net flow but am just too dense to see what is going on.
I have code:
private void CancelCalibration()
{
// ...
TaskResult closeDoorResult = CloseLoadDoor().ConfigureAwait(false).GetAwaiter().GetResult();
CalibrationState = CalibrationState.Idle;
return;
// ...
}
private async Task<TaskResult> CloseLoadDoor()
{
TaskResult result = await _model.CloseLoadDoor().ConfigureAwait(false);
return result;
}
public async Task<TaskResult> CloseLoadDoor()
{
TaskResult result = new TaskResult()
{
Explanation = "",
Success = true
};
await _robotController.CloseLoadDoors().ConfigureAwait(false);
return result;
}
public async Task CloseLoadDoors()
{
await Task.Run(() => _robot.CloseLoadDoors());
}
public void CloseLoadDoors()
{
// syncronous code from here down
_doorController.CloseLoadDoors(_operationsManager.GetLoadDoorCalibration());
}
As you can see, CloseLoadDoor is declared async. I thought (especially from the first article above) that if I use ConfigureAwait(false) I could call an async method without a deadlock. But that is what I appear to get. The call to "CloseLoadDoor().ConfigureAwait(false).GetAwaiter().GetResult() never returns!
I'm using the GetAwaiter.GetResult because CancelCalibration is NOT an async method. It's a button handler defined via an MVVM pattern:
public ICommand CancelCalibrationCommand
=> _cancelCalibrationCommand ?? (_cancelCalibrationCommand = new DelegateCommand(CancelCalibration));
If someone is going to tell me that I can make CancelCalibration async, please tell me how. Can I just add async
to the method declaration? HOWEVER, I'd still like to know why the ConfigureAwait.GetAwaiter.GetResult
pattern is giving me trouble. My understanding was that GetAwaiter.GetResult
was a way to call async method from syncronous methods when changing the signature is not an option.
I'm guessing I'm not really freeing myself from using the original context, but what am I doing wrong and what is the pattern to fix it? Thanks, Dave