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);
        }
Beispiel #2
0
        public static Future <T> Bind <T> (this Future <T> future, Expression <Func <T> > target)
        {
            var member = BoundMember.New(target);

            future.RegisterOnComplete((_) => {
                T result;
                if (future.GetResult(out result))
                {
                    member.Value = result;
                }
            });

            return(future);
        }
Beispiel #3
0
        public static IEnumerator <object> EnumerateViaThreadpool <T> (IEnumerator <T> enumerator, int blockSize)
        {
            using (new Disposer(enumerator)) {
                var buffer = new List <T>(blockSize);
                var nv     = new NextValue(null);

                WaitCallback moveNext = (state) => {
                    var f = (Future <bool>)state;
                    try {
                        while (buffer.Count < blockSize)
                        {
                            if (enumerator.MoveNext())
                            {
                                buffer.Add(enumerator.Current);
                            }
                            else
                            {
                                f.SetResult(true, null);
                                return;
                            }
                        }

                        f.SetResult(false, null);
                    } catch (Exception e) {
                        f.SetResult(false, e);
                    }
                };

                while (true)
                {
                    buffer.Clear();
                    var f = new Future <bool>();
                    ThreadPool.QueueUserWorkItem(moveNext, f);
                    yield return(f);

                    bool      atEnd;
                    Exception error;
                    if (f.GetResult(out atEnd, out error))
                    {
                        nv.Value = buffer;
                        yield return(nv);

                        if (atEnd)
                        {
                            yield break;
                        }
                    }
                }
            }
        }