我该怎么做相当于 TaskCompletionSource<nothing>?
How do I do the equivalent of TaskCompletionSource<nothing>?
我想要一个EventWaitHandle-like Task where I await on it on one end, and then just set it to completed on the other end. TaskCompletionSource几乎是我想要的,但它需要我传递一个值。我可以只使用一个虚拟值,但在我这样做之前,我想我会看看是否有更正确的解决方案。
重点是能够使用Task.WaitAny。
我在源代码中看到的最常见的情况是简单地创建一个空的struct
,这相当于什么都没有。或者使用 bool
作为虚拟对象:
internal struct Void { }
然后:
var tcs = new TaskCompletionSource<Void>();
我也在 BCL source code 中看到了这种常见模式:
// Special internal struct that we use to signify that we are not interested in
// a Task<VoidTaskResult>'s result.
internal struct VoidTaskResult { }
我想要一个EventWaitHandle-like Task where I await on it on one end, and then just set it to completed on the other end. TaskCompletionSource几乎是我想要的,但它需要我传递一个值。我可以只使用一个虚拟值,但在我这样做之前,我想我会看看是否有更正确的解决方案。
重点是能够使用Task.WaitAny。
我在源代码中看到的最常见的情况是简单地创建一个空的struct
,这相当于什么都没有。或者使用 bool
作为虚拟对象:
internal struct Void { }
然后:
var tcs = new TaskCompletionSource<Void>();
我也在 BCL source code 中看到了这种常见模式:
// Special internal struct that we use to signify that we are not interested in
// a Task<VoidTaskResult>'s result.
internal struct VoidTaskResult { }