Non-Generic TaskCompletionSource or alternative
.NET 5 has a non-generic TaskCompletionSource
.
It was added in this pull request: https://github.com/dotnet/runtime/pull/37452/files#diff-4a72dcb26e2d643c337baef9f64312f3
If you don't want to leak information, the common approach is to use TaskCompletionSource<object>
and complete with a result of null
. Then just return it as a Task
.
The method can be changed to:
public Task ShowAlert(object message, string windowTitle)
Task<bool>
inherits from Task
so you can return Task<bool>
while only exposing Task
to the caller
Edit:
I found a Microsoft document, http://www.microsoft.com/en-us/download/details.aspx?id=19957, by Stephen Toub titled 'The Task-based Asynchronous pattern' and it has the following excerpt that recommends this same pattern.
There is no non-generic counterpart to TaskCompletionSource<TResult>. However, Task<TResult> derives from Task, and thus the generic TaskCompletionSource<TResult> can be used for I/O-bound methods that simply return a Task by utilizing a source with a dummy TResult (Boolean is a good default choice, and if a developer is concerned about a consumer of the Task downcasting it to a Task<TResult>, a private TResult type may be used)