run async methods within a sync process
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();
}
Also in C#:
- how to use variables in c#
- slomotion in unity 3d
- get number of sundays in a month c#
- countdown script in unity
- c# multi threading example
- find month number from date C#
- unity calculate path
- C# webclient submit form
- uncaught syntaxerror invalid or unexpected token unity webgl
- s3 upload base64 .net core
- verifyusertokenasync password reset token
- open udp socket c#
- c# enum variable set to nonthing
- linq in c#
- c# initialize array
- check if two timespans intersect c#
- wpf binding ancestor codebehind
- C# Cast double to float
- unity get velocity of gameobject
- c# switct case
- get string character by index c#
- c# string
- value is null to insert in c#
- unity move character