PromiseHelper.cs
40 lines
| 1.1 KiB
| text/x-csharp
|
CSharpLexer
/ Implab.Test / PromiseHelper.cs
cin
|
r249 | using Implab; | ||
using System; | ||||
cin
|
r10 | using System.Threading; | ||
namespace Implab.Test { | ||||
cin
|
r77 | static class PromiseHelper { | ||
cin
|
r249 | 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); | ||||
cin
|
r10 | }); | ||
} | ||||
cin
|
r203 | |||
cin
|
r249 | 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); | ||||
cin
|
r203 | }); | ||
} | ||||
cin
|
r10 | } | ||
} | ||||