mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 19:25:01 +00:00
b8b27f6885
* WPF/OffScreen - Add WaitForRenderIdleAsync - Task resolves when render has been idle for the specified time - Make TaskTimeoutExtensions public (they're not extension methods as this was expected)
38 lines
1.9 KiB
C#
38 lines
1.9 KiB
C#
// https://github.com/dotnet/runtime/blob/933988c35c172068652162adf6f20477231f815e/src/libraries/Common/tests/System/Threading/Tasks/TaskTimeoutExtensions.cs#L1
|
|
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// https://github.com/dotnet/runtime/blob/933988c35c172068652162adf6f20477231f815e/src/libraries/Common/tests/System/Threading/Tasks/TaskTimeoutExtensions.cs#L12
|
|
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CefSharp.Internals
|
|
{
|
|
/// <summary>
|
|
/// WaitAsync polyfills imported from .Net Runtime
|
|
/// as we don't get access to this method in older .net versions
|
|
/// </summary>
|
|
public static class TaskTimeoutExtensions
|
|
{
|
|
public static Task<TResult> WaitAsync<TResult>(Task<TResult> task, int millisecondsTimeout) =>
|
|
WaitAsync(task, TimeSpan.FromMilliseconds(millisecondsTimeout), default);
|
|
|
|
public static Task<TResult> WaitAsync<TResult>(Task<TResult> task, TimeSpan timeout) =>
|
|
WaitAsync(task, timeout, default);
|
|
|
|
public static Task<TResult> WaitAsync<TResult>(Task<TResult> task, CancellationToken cancellationToken) =>
|
|
WaitAsync(task, Timeout.InfiniteTimeSpan, cancellationToken);
|
|
|
|
public static async Task<TResult> WaitAsync<TResult>(Task<TResult> task, TimeSpan timeout, CancellationToken cancellationToken)
|
|
{
|
|
var tcs = new TaskCompletionSource<TResult>();
|
|
using (new Timer(s => ((TaskCompletionSource<TResult>)s).TrySetException(new TimeoutException()), tcs, timeout, Timeout.InfiniteTimeSpan))
|
|
using (cancellationToken.Register(s => ((TaskCompletionSource<TResult>)s).TrySetCanceled(), tcs))
|
|
{
|
|
return await (await Task.WhenAny(task, tcs.Task).ConfigureAwait(false)).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|
|
}
|