view Implab.Test/PromiseHelper.cs @ 266:254d1f255d87 v3

Добавлена метка v3.0.10 для набора изменений 74e048cbaac8
author cin
date Mon, 16 Apr 2018 19:45:18 +0300
parents d82909310094
children
line wrap: on
line source

using Implab;
using System;
using System.Threading;

namespace Implab.Test {
    static class PromiseHelper {
        public static IPromise<T> Sleep<T>(int timeout, T retVal, CancellationToken ct = default(CancellationToken)) {

            Timer timer = null;

            return Promise.Create<T>((d) => {
                timer = new Timer(x => {
                    d.Resolve(retVal);
                }, null, timeout, Timeout.Infinite);

                if(ct.CanBeCanceled)
                    ct.Register(d.Cancel);

            }).Finally(() => {
                Safe.Dispose(timer);
            });
        }

        public static IPromise Sleep(int timeout, CancellationToken ct = default(CancellationToken)) {
            Timer timer = null;

            return Promise.Create((d) => {
                timer = new Timer(x => {
                    d.Resolve();
                }, null, timeout, Timeout.Infinite);

                if(ct.CanBeCanceled)
                    ct.Register(d.Cancel);

            }).Finally(() => {
                Safe.Dispose(timer);
            });
        }
    }
}