Search code examples
c#.nettask-parallel-library

How do I do the equivalent of TaskCompletionSource<nothing>?


I want to have a EventWaitHandle-like Task where I await on it on one end, and then just set it to completed on the other end. TaskCompletionSource is almost what I want, but it requires me to pass a value. I could just use a dummy value, but before I do that I thought I'd see if there's a more correct solution.

The point is to be able to use Task.WaitAny.


Solution

  • Starting from .NET 5, a non-generic TaskCompletionSource class is available in .NET standard libraries.

    public class TaskCompletionSource
    

    It has exactly the same functionality with the TaskCompletionSource<TResult> class, apart from the TResult.