| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System;
- using System.Reflection;
- using System.Threading.Tasks;
- using Nito.AsyncEx;
- namespace Abp.Threading
- {
- /// <summary>
- /// Provides some helper methods to work with async methods.
- /// </summary>
- public static class AsyncHelper
- {
- /// <summary>
- /// Checks if given method is an async method.
- /// </summary>
- /// <param name="method">A method to check</param>
- public static bool IsAsync(this MethodInfo method)
- {
- return (
- method.ReturnType == typeof(Task) ||
- (method.ReturnType.GetTypeInfo().IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
- );
- }
- /// <summary>
- /// Checks if given method is an async method.
- /// </summary>
- /// <param name="method">A method to check</param>
- [Obsolete("Use MethodInfo.IsAsync() extension method!")]
- public static bool IsAsyncMethod(MethodInfo method)
- {
- return method.IsAsync();
- }
- /// <summary>
- /// Runs a async method synchronously.
- /// </summary>
- /// <param name="func">A function that returns a result</param>
- /// <typeparam name="TResult">Result type</typeparam>
- /// <returns>Result of the async operation</returns>
- public static TResult RunSync<TResult>(Func<Task<TResult>> func)
- {
- return AsyncContext.Run(func);
- }
- /// <summary>
- /// Runs a async method synchronously.
- /// </summary>
- /// <param name="action">An async action</param>
- public static void RunSync(Func<Task> action)
- {
- AsyncContext.Run(action);
- }
- }
- }
|