1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-15 18:33:06 +00:00

Added custom async wait event implementation as the previously used method seemed to not work 100% of the time

This commit is contained in:
Jkorf
2021-10-05 14:31:27 +02:00
parent 4ef20a9637
commit f3102a4dad
7 changed files with 282 additions and 229 deletions
-53
View File
@@ -191,59 +191,6 @@ namespace CryptoExchange.Net
return secureString;
}
/// <summary>
/// Wait one async
/// </summary>
/// <param name="handle"></param>
/// <param name="millisecondsTimeout"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<bool> WaitOneAsync(this WaitHandle handle, int millisecondsTimeout, CancellationToken cancellationToken)
{
RegisteredWaitHandle? registeredHandle = null;
CancellationTokenRegistration tokenRegistration = default;
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.ConfigureAwait(false);
}
finally
{
registeredHandle?.Unregister(null);
tokenRegistration.Dispose();
}
}
/// <summary>
/// Wait one async
/// </summary>
/// <param name="handle"></param>
/// <returns></returns>
public static Task<bool> WaitOneAsync(this WaitHandle handle)
{
return handle.WaitOneAsync(-1, CancellationToken.None);
}
/// <summary>
/// Wait one async
/// </summary>
/// <param name="handle"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public static Task<bool> WaitOneAsync(this WaitHandle handle, TimeSpan timeout)
{
return handle.WaitOneAsync((int)timeout.TotalMilliseconds, CancellationToken.None);
}
/// <summary>
/// String to JToken
/// </summary>