Created
February 10, 2016 15:17
-
-
Save HaloFour/94e6a79e79413bdeddd3 to your computer and use it in GitHub Desktop.
SynchronizationContext Awaiter Extension Method
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
public static class SynchronizationContextExtensions { | |
public struct SynchronizationContextAwaiter : INotifyCompletion { | |
private readonly SynchronizationContext context; | |
public SynchronizationContextAwaiter(SynchronizationContext context) { | |
this.context = context; | |
} | |
public bool IsCompleted { | |
get { return (context == null); } | |
} | |
public void GetResult() { } | |
public void OnCompleted(Action continuation) { | |
if (context == null) { | |
continuation(); | |
} | |
else { | |
context.Send(state => continuation(), null); | |
} | |
} | |
} | |
public static SynchronizationContextAwaiter GetAwaiter(this SynchronizationContext context) { | |
return new SynchronizationContextAwaiter(context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment