using System;
using System.Threading;
using System.Threading.Tasks;
using Mesnac.Log;

namespace HighWayAssemble
{/// <summary>
/// 扩展 异步方法
/// </summary>
    public class AsyncHelper
    {
        public static Task Run(Action action)
        {
            var tcs = new TaskCompletionSource<object>();
            new Thread(() =>
            {
                try
                {
                    action();
                  //  tcs.SetResult(null);
                }
                catch (Exception ex)
                {
                    //  tcs.SetException(ex);
                    LogService.Instance.Error(ex);
                }
            })
            { IsBackground = true }.Start();
            return tcs.Task;
        }
        public static Task Delay(int milliseconds)
        {
            var tcs = new TaskCompletionSource<object>();
            var timer = new System.Timers.Timer(milliseconds) { AutoReset = false };
            timer.Elapsed += delegate { timer.Dispose(); tcs.SetResult(null); };
            timer.Start();
            return tcs.Task;
        }
    }
}
