public static Task WaitAnyAsync(WaitHandle handle1, WaitHandle handle2, TimeSpan timeout) { var t1 = handle1.WaitOneAsync(timeout); var t2 = handle2.WaitOneAsync(timeout); return(Task.WhenAny(t1, t2)); }
/// <summary> /// Asynchronously waits for the wait handle. /// </summary> /// <param name="handle">The handle.</param> /// <param name="millisecondsTimeout">The timeout, in milliseconds.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns><c>true</c> if the timeout has not been reached, <c>false</c> otherwise.</returns> public static ValueTask <bool> WaitOneAsync( this WaitHandle handle, double millisecondsTimeout, CancellationToken cancellationToken) => handle.WaitOneAsync( TimeSpan.FromMilliseconds(millisecondsTimeout), cancellationToken);
/// <summary> /// Blocks until <paramref name="duration"/> elapses or <paramref name="waitHandle"/> is canceled, /// whichever comes first. /// </summary> /// <param name="waitHandle">The <see cref="WaitHandle"/>.</param> /// <param name="duration">The time to sleep.</param> /// <returns><see langword="true"/> if the thread slept for <paramref name="duration"/>, <see langword="false"/> if <paramref name="waitHandle"/> was canceled.</returns> /// <exception cref="T:System.Runtime.InteropServices.SEHException">An unknown error occurred while accessing the <see cref="WaitHandle"/>.</exception> public static async Task <bool> IsNotCancelledAfterAsync(this WaitHandle waitHandle, TimeSpan duration) { try { return(!await waitHandle.WaitOneAsync(duration).ConfigureAwait(false)); } catch (AbandonedMutexException) { return(false); } catch (ObjectDisposedException) { return(false); } catch (SEHException e) when(e.Message.IndexOf("0x80004005", StringComparison.OrdinalIgnoreCase) >= 0) { // This error is likely due to anti-tampering technology or antivirus software. // Nothing we can really do about it. throw; } }
/// <summary> /// Allows awaiting a <see cref="WaitHandle"/> /// </summary> /// <param name="handle">The handle to await</param> /// <param name="cancellationToken">The cancellation token to use to throw a <see cref="TaskCanceledException"/> if this token gets canceled</param> /// <returns>Returns true if the handle is free, or waits infinitely until it is free or the cancellation token is canceled</returns> /// <exception cref="TaskCanceledException">Throws if the cancellation token is canceled</exception> public static Task <bool> WaitOneAsync(this WaitHandle handle, CancellationToken?cancellationToken = null) { return(handle.WaitOneAsync(Timeout.Infinite, cancellationToken)); }
/// <summary> /// Allows awaiting a <see cref="WaitHandle"/> /// </summary> /// <param name="handle">The handle to await</param> /// <param name="timeout">The timeout period to return false if timed out</param> /// <param name="cancellationToken">The cancellation token to use to throw a <see cref="TaskCanceledException"/> if this token gets canceled</param> /// <returns>Returns true if the handle is free, false if it is not</returns> /// <exception cref="TaskCanceledException">Throws if the cancellation token is canceled</exception> public static Task <bool> WaitOneAsync(this WaitHandle handle, TimeSpan timeout, CancellationToken?cancellationToken = null) { return(handle.WaitOneAsync((int)timeout.TotalMilliseconds, cancellationToken)); }
public static Task <bool> WaitOneAsync(this WaitHandle handle, CancellationToken cancellationToken) => handle.WaitOneAsync(Timeout.Infinite, cancellationToken);
public static Task <bool> WaitOneAsync(this WaitHandle handle, TimeSpan timeout, CancellationToken cancellationToken) => handle.WaitOneAsync((int)timeout.TotalMilliseconds, cancellationToken);
internal static Task <bool> WaitOneAsync(this WaitHandle handle, CancellationToken cancellationToken) { return(handle.WaitOneAsync(Timeout.Infinite, cancellationToken)); }
public static async Task <APIResponse <Bitmap> > GetAvatar(string userID) { // If avatars contains the userID key, the download is either in progress, or has completed if (avatars.ContainsKey(userID)) { object avatar = avatars[userID]; // If the object is an APIResponse, return it if (avatar is APIResponse <Bitmap> ) { return((APIResponse <Bitmap>)avatar); } else { // If not, its a WaitHandle, so cast it and await it WaitHandle handle = (WaitHandle)avatar; await handle.WaitOneAsync(); return((APIResponse <Bitmap>)avatars[userID]); } } EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.ManualReset); avatars.Add(userID, ewh); try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(APIBaseURL, string.Format("users/{0}/image", userID))); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token); bool rateLimited = false; HttpResponseMessage response = null; while (!rateLimited) { response = await client.SendAsync(request); if ((int)response.StatusCode == 429) { await Task.Delay(1000); } else { rateLimited = true; } } APIResponse <Bitmap> retVal; if (response.StatusCode != HttpStatusCode.OK) { string responseContent = await response.Content.ReadAsStringAsync(); APIErrorResponse error = JsonConvert.DeserializeObject <APIErrorResponse>(responseContent); retVal = new APIResponse <Bitmap>() { Success = false, Error = error.Message }; avatars[userID] = retVal; ewh.Set(); return(retVal); } Bitmap avatar = new Bitmap(await response.Content.ReadAsStreamAsync()); retVal = new APIResponse <Bitmap>() { Success = true, Value = avatar.ApplyMask(Mattermost.Properties.Resources.AvatarMask) }; avatars[userID] = retVal; ewh.Set(); return(retVal); } catch (Exception e) { APIResponse <Bitmap> retVal = new APIResponse <Bitmap>() { Success = false, Error = e.Message }; avatars[userID] = retVal; ewh.Set(); return(retVal); } }
public static Task <bool> WaitOneAsync(this WaitHandle handle, int timeout) { return(handle.WaitOneAsync(timeout, CancellationToken.None)); }
/// <summary> /// Extension method to make WaitOne awaitable /// </summary> /// <param name="waitHandle">WaitHandle</param> /// <param name="timeout">Timeout for waiting</param> /// <returns></returns> public static Task <bool> WaitOneAsync(this WaitHandle waitHandle, TimeSpan timeout) { return(waitHandle.WaitOneAsync((int)timeout.TotalMilliseconds, new CancellationTokenSource().Token)); }
/// <summary> /// Async version of WaitOne taken from the following blog: /// https://thomaslevesque.com/2015/06/04/async-and-cancellation-support-for-wait-handles/ /// </summary> public static Task <bool> WaitOneAsync(this WaitHandle objHandle, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return(objHandle.WaitOneAsync(Timeout.Infinite, token)); }
/// <summary> /// Async version of WaitOne taken from the following blog: /// https://thomaslevesque.com/2015/06/04/async-and-cancellation-support-for-wait-handles/ /// </summary> public static Task <bool> WaitOneAsync(this WaitHandle objHandle, TimeSpan objTimeout, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return(objHandle.WaitOneAsync((int)objTimeout.TotalMilliseconds, token)); }
/// <summary> /// 非同期に待機します。 /// </summary> /// <param name="handle">待機対象の<see cref="WaitHandle"/>オブジェクトです。</param> /// <returns>オブジェクトがシグナル状態になった場合にはtrueで完了するタスク、そうではない場合にはfalseで完了するタスクです。</returns> public static Task <bool> WaitOneAsync(this WaitHandle handle) { return(handle.WaitOneAsync(Timeout.Infinite, CancellationToken.None)); }