using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase.Logging;
namespace SuperSocket.SocketBase
{
///
/// Async extension class
///
public static class Async
{
///
/// Runs the specified task.
///
/// The log provider.
/// The task.
///
public static Task AsyncRun(this ILoggerProvider logProvider, Action task)
{
return AsyncRun(logProvider, task, TaskCreationOptions.None);
}
///
/// Runs the specified task.
///
/// The log provider.
/// The task.
/// The task option.
///
public static Task AsyncRun(this ILoggerProvider logProvider, Action task, TaskCreationOptions taskOption)
{
return AsyncRun(logProvider, task, taskOption, null);
}
///
/// Runs the specified task.
///
/// The log provider.
/// The task.
/// The exception handler.
///
public static Task AsyncRun(this ILoggerProvider logProvider, Action task, Action exceptionHandler)
{
return AsyncRun(logProvider, task, TaskCreationOptions.None, exceptionHandler);
}
///
/// Runs the specified task.
///
/// The log provider.
/// The task.
/// The task option.
/// The exception handler.
///
public static Task AsyncRun(this ILoggerProvider logProvider, Action task, TaskCreationOptions taskOption, Action exceptionHandler)
{
return Task.Factory.StartNew(task, taskOption).ContinueWith(t =>
{
if (exceptionHandler != null)
exceptionHandler(t.Exception);
else
{
if (logProvider.Logger.IsErrorEnabled)
{
for (var i = 0; i < t.Exception.InnerExceptions.Count; i++)
{
logProvider.Logger.Error(t.Exception.InnerExceptions[i]);
}
}
}
}, TaskContinuationOptions.OnlyOnFaulted);
}
///
/// Runs the specified task.
///
/// The log provider.
/// The task.
/// The state.
///
public static Task AsyncRun(this ILoggerProvider logProvider, Action