Skip to content

Instantly share code, notes, and snippets.

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