I'm working with an alert window (Telerik WPF) that is normally displayed asynchronously ( code continues running while it is open) and I want to make it synchronous by using async/await.
I have this working with TaskCompletionSource
but that class is generic and returns an object like Task<bool>
when all I want is a plain Task
with no return value.
public Task<bool> ShowAlert(object message, string windowTitle)
{
var dialogParameters = new DialogParameters { Content = message };
var tcs = new TaskCompletionSource<bool>();
dialogParameters.Closed += (s, e) => tcs.TrySetResult(true);
RadWindow.Alert(dialogParameters);
return tcs.Task;
}
The code that calls that method is
await MessageBoxService.ShowAlert("The alert text.")
How can I return a non-generic Task that functions similarly which I can await until the dialogParameters.Closed
event fires? I understand that I could just ignore the bool
that is being returned in this code. I am looking for a different solution than that.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…