Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class WaitHandleAsyncExtension
- {
- public static async Task<bool> WaitOneAsync(this WaitHandle handle, int millisecondsTimeout, CancellationToken cancellationToken)
- {
- RegisteredWaitHandle registeredHandle = null;
- var tokenRegistration = new CancellationTokenRegistration();
- try
- {
- var tcs = new TaskCompletionSource<bool>();
- registeredHandle = ThreadPool.RegisterWaitForSingleObject(
- handle,
- (state, timedOut) => ((TaskCompletionSource<bool>)state).TrySetResult(!timedOut),
- tcs,
- millisecondsTimeout,
- true);
- tokenRegistration = cancellationToken.Register(
- state => ((TaskCompletionSource<bool>)state).TrySetCanceled(),
- tcs);
- return await tcs.Task;
- }
- finally
- {
- registeredHandle?.Unregister(null);
- tokenRegistration.Dispose();
- }
- }
- public static Task<bool> WaitOneAsync(this WaitHandle handle, TimeSpan timeout, CancellationToken cancellationToken)
- {
- return handle.WaitOneAsync((int)timeout.TotalMilliseconds, cancellationToken);
- }
- public static Task<bool> WaitOneAsync(this WaitHandle handle, CancellationToken cancellationToken)
- {
- return handle.WaitOneAsync(Timeout.Infinite, cancellationToken);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement