Non-Generic TaskCompletionSource or alternative

后端 未结 5 1084
时光说笑
时光说笑 2020-12-05 12:48

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 usi

5条回答
  •  粉色の甜心
    2020-12-05 13:20

    The method can be changed to:

    public Task ShowAlert(object message, string windowTitle)
    

    Task inherits from Task so you can return Task 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. However, Task derives from Task, and thus the generic TaskCompletionSource 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, a private TResult type may be used)

提交回复
热议问题