C# TaskCompletionSource / Promise Resolve

Date: 2024-04-25
{
    private TaskCompletionSource<bool> SomethingChangedCompletionSource = new TaskCompletionSource<bool>();
    
    public Task GetAwaitSomethingChangedTask()
    {
        // with a timeout of 60 seconds?
        return Task.WhenAny(Task.Delay(60_000), SomethingChangedCompletionSource.Task);
    }
    
    public void MarkSomethingChanged()
    {
        SomethingChangedCompletionSource.SetResult(true);
        SomethingChangedCompletionSource = new TaskCompletionSource<bool>();
    }
}
// usage:
// wait somewhere for something changed
await GetAwaitSomethingChangedTask();
84890cookie-checkC# TaskCompletionSource / Promise Resolve