init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
46
Kreta.Core/AsyncUtil.cs
Normal file
46
Kreta.Core/AsyncUtil.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Kreta.Core
|
||||
{
|
||||
//https://cpratt.co/async-tips-tricks/
|
||||
//https://www.ryadel.com/en/asyncutil-c-helper-class-async-method-sync-result-wait/
|
||||
/// <summary>
|
||||
/// Helper class to run async methods within a sync process.
|
||||
/// </summary>
|
||||
public static class AsyncUtil
|
||||
{
|
||||
private static readonly TaskFactory _taskFactory = new
|
||||
TaskFactory(CancellationToken.None,
|
||||
TaskCreationOptions.None,
|
||||
TaskContinuationOptions.None,
|
||||
TaskScheduler.Default);
|
||||
|
||||
/// <summary>
|
||||
/// Executes an async Task method which has a void return value synchronously
|
||||
/// USAGE: AsyncUtil.RunSync(() => AsyncMethod());
|
||||
/// </summary>
|
||||
/// <param name="task">Task method to execute</param>
|
||||
public static void RunSync(Func<Task> task)
|
||||
=> _taskFactory
|
||||
.StartNew(task)
|
||||
.Unwrap()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
/// <summary>
|
||||
/// Executes an async Task<T> method which has a T return type synchronously
|
||||
/// USAGE: T result = AsyncUtil.RunSync(() => AsyncMethod<T>());
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">Return Type</typeparam>
|
||||
/// <param name="task">Task<T> method to execute</param>
|
||||
/// <returns></returns>
|
||||
public static TResult RunSync<TResult>(Func<Task<TResult>> task)
|
||||
=> _taskFactory
|
||||
.StartNew(task)
|
||||
.Unwrap()
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue