Last active
July 20, 2023 21:25
-
-
Save jnm2/5638603bc1f5d870c153d359eebaa5e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
internal sealed class DeferredAction | |
{ | |
private static readonly Action SentinelNextSetShouldPost = () => { }; | |
private readonly SynchronizationContext synchronizationContext; | |
private Action scheduledAction = SentinelNextSetShouldPost; | |
public DeferredAction() | |
{ | |
synchronizationContext = SynchronizationContext.Current | |
?? throw new InvalidOperationException($"{nameof(DeferredAction)} requires a current synchronization context."); | |
} | |
public void Set(Action action) | |
{ | |
var previouslyScheduledAction = Interlocked.Exchange(ref scheduledAction, action); | |
if (ReferenceEquals(previouslyScheduledAction, SentinelNextSetShouldPost)) | |
synchronizationContext.Post(OnSynchronizationContextPost, state: this); | |
} | |
public void Clear() => Set(null); | |
private static void OnSynchronizationContextPost(object state) | |
{ | |
var @this = (DeferredAction)state; | |
Interlocked.Exchange(ref @this.scheduledAction, SentinelNextSetShouldPost)?.Invoke(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment