# HG changeset patch # User cin # Date 2014-11-15 23:49:12 # Node ID 1a8426e6e895a6f625001fc48706581e31fcd1a0 # Parent 1b7ebcc52e5a43ffde8efae07336f938b9f3cefe added promise timeout helper diff --git a/Implab/PromiseExtensions.cs b/Implab/PromiseExtensions.cs --- a/Implab/PromiseExtensions.cs +++ b/Implab/PromiseExtensions.cs @@ -72,6 +72,23 @@ namespace Implab { } }; } + + static void CancelCallback(object cookie) { + ((ICancellable)cookie).Cancel(); + } + + /// + /// Cancells promise after the specified timeout is elapsed. + /// + /// The promise to cancel on timeout. + /// The timeout in milliseconds. + /// The 1st type parameter. + public static TPromise Timeout(this TPromise that, int milliseconds) where TPromise : IPromise { + Safe.ArgumentNotNull(that, "that"); + var timer = new Timer(CancelCallback, that, milliseconds, -1); + that.On(timer.Dispose, PromiseEventType.All); + return that; + } #if NET_4_5