mirror of
https://github.com/sphildreth/roadie
synced 2024-11-29 07:30:21 +00:00
43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Roadie.Library.Utility
|
|
{
|
|
public static class AsyncHelper
|
|
{
|
|
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: AsyncHelper.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 = AsyncHelper.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();
|
|
}
|
|
}
|