Skip to content

Instantly share code, notes, and snippets.

@peteraritchie
Created June 28, 2012 16:21
Show Gist options
  • Save peteraritchie/3012271 to your computer and use it in GitHub Desktop.
Save peteraritchie/3012271 to your computer and use it in GitHub Desktop.
WaitOneAsync
public static Task WaitOneAsync(this WaitHandle waitHandle)
{
if (waitHandle == null) throw new ArgumentNullException("waitHandle");
var tcs = new TaskCompletionSource<bool>();
var rwh = ThreadPool.RegisterWaitForSingleObject(waitHandle,
delegate { tcs.TrySetResult(true); }, null, -1, true);
var t = tcs.Task;
t.ContinueWith(_ => rwh.Unregister(null));
return t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment