using System;
using System.Threading;
using System.Threading.Tasks;

namespace VCommon.Logging.Viewer.Utility
{
    /// <summary>
    /// 延迟执行
    /// </summary>
    public static class TaskExtend
    {
        
        public static Task Delay(int dueTime)
        {
            if (dueTime < -1) throw new ArgumentOutOfRangeException(nameof(dueTime));
            Timer timer = null;
            var source = new TaskCompletionSource<bool>();
            timer = new Timer(_ =>
            {
                // ReSharper disable once AccessToModifiedClosure
                using (timer) source.TrySetResult(true);
            }, null, dueTime, Timeout.Infinite);

            return source.Task;
        }
    }
}