protected void EventHandler(EventInfo e) { if (_IsCompleted) { return; } // Events that don't match are silently rejected. // FIXME: Is this actually horrible if (!TryExtractResult(e, out Result)) { return; } e.Consume(); _IsCompleted = true; Dispose(); if (Continuation != null) { Continuation(); } if (_Future != null) { _Future.SetResult(Result, null); } }
public void TestGetResultMethodNeverThrows() { var f = new Future <object>(); object result; Exception error; Assert.IsFalse(f.GetResult(out result, out error)); f.SetResult(5, null); Assert.IsTrue(f.GetResult(out result, out error)); Assert.AreEqual(5, result); f = new Future <object>(); f.SetResult(null, new Exception("earth-shattering kaboom")); Assert.IsTrue(f.GetResult(out result, out error)); Assert.IsTrue(error is Exception); }
public static void Fail <T> (this Future <T> future, Exception error) { future.SetResult(default(T), error); }
public static void Complete <T> (this Future <T> future, T result) { future.SetResult(result, null); }
IEnumerator <object> RunAsBackgroundTestTask(Future <int> fOut) { yield return(new RunAsBackground(CrashyTask())); fOut.SetResult(5, null); }
public static IEnumerator <object> IssueRequest(HttpWebRequest request) { var fResponse = new Future <HttpWebResponse>(); ThreadPool.QueueUserWorkItem( (__) => { try { request.BeginGetResponse( (ar) => { try { var _ = (HttpWebResponse)request.EndGetResponse(ar); fResponse.SetResult(_, null); } catch (Exception ex) { fResponse.SetResult(null, ex); } }, null ); } catch (Exception ex_) { fResponse.SetResult(null, ex_); } } ); yield return(fResponse); if (fResponse.Failed) { throw new RequestFailedException(fResponse.Error); } using (var response = fResponse.Result) { var fResponseStream = Future.RunInThread( () => response.GetResponseStream() ); yield return(fResponseStream); Encoding encoding = AsyncTextReader.DefaultEncoding; if (!string.IsNullOrEmpty(response.CharacterSet)) { encoding = Encoding.GetEncoding(response.CharacterSet); } string responseText; using (var stream = fResponseStream.Result) using (var adapter = new AsyncTextReader(new StreamDataAdapter(stream, false), encoding)) { var fText = adapter.ReadToEnd(); yield return(fText); responseText = fText.Result; } var cookies = new Cookie[response.Cookies.Count]; response.Cookies.CopyTo(cookies, 0); yield return(new Result(new Response { Body = responseText, ContentType = response.ContentType, StatusCode = response.StatusCode, StatusDescription = response.StatusDescription, Cookies = cookies })); } }