comparison Implab/PromiseExtensions.cs @ 110:1a8426e6e895 v2

added promise timeout helper
author cin
date Sun, 16 Nov 2014 02:49:12 +0300
parents 1b7ebcc52e5a
children 2573b562e328
comparison
equal deleted inserted replaced
109:1b7ebcc52e5a 110:1a8426e6e895
70 } finally { 70 } finally {
71 TraceContext.Instance.Leave(); 71 TraceContext.Instance.Leave();
72 } 72 }
73 }; 73 };
74 } 74 }
75
76 static void CancelCallback(object cookie) {
77 ((ICancellable)cookie).Cancel();
78 }
79
80 /// <summary>
81 /// Cancells promise after the specified timeout is elapsed.
82 /// </summary>
83 /// <param name="that">The promise to cancel on timeout.</param>
84 /// <param name="milliseconds">The timeout in milliseconds.</param>
85 /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
86 public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise {
87 Safe.ArgumentNotNull(that, "that");
88 var timer = new Timer(CancelCallback, that, milliseconds, -1);
89 that.On(timer.Dispose, PromiseEventType.All);
90 return that;
91 }
75 92
76 #if NET_4_5 93 #if NET_4_5
77 94
78 public static Task<T> GetTask<T>(this IPromise<T> that) { 95 public static Task<T> GetTask<T>(this IPromise<T> that) {
79 Safe.ArgumentNotNull(that, "that"); 96 Safe.ArgumentNotNull(that, "that");