Exemplo n.º 1
0
        public void CatchThrowFinally()
        {
            var x = 42;
            var y = x * x;

            Corout.Create <int>(co => Routines.Failing(x, co))
            .OnCatch <NotSupportedException>((ex, co) =>
            {
                Assert.IsInstanceOf <NotSupportedException>(ex);
                Debug.Log("Catch: 'NotSupportedException'.");
            })
            .OnFinally(co =>
            {
                Assert.IsTrue(co.Faulted);
                Debug.Log("Finally do some clean-up...");
            })
            .Start();


            Corout.Create <int>(co => Routines.GetIntSquareRoot(x, co))
            .OnFinally(co =>
            {
                Debug.Log("'OnFinally' doesn't need 'OnCatch' and are always executed.");
                Assert.AreEqual(co.Result, y);
                Assert.IsTrue(co.Succeeded);
            })
            .Start();


            Corout.Create <int>(co => Routines.Failing(x, co))
            .OnCatch <NotSupportedException>((ex, co) =>
            {
                Assert.IsInstanceOf <NotSupportedException>(ex);
                Assert.IsTrue(co.Faulted);
                throw ex;
            })
            .OnFinally(co =>
            {
                Assert.IsTrue(co.Faulted);
                Debug.Log("Even if you throw exception 'OnFinally' is executed.");
            })
            .Start();
        }
Exemplo n.º 2
0
        public void Interlace()
        {
            var x = 3;
            var y = 4.57f;
            var z = 1425.67f;

            var xx = x * x;
            var yy = y * y;
            var zz = z.ToString();

            var cw = string.Join(" ", new string[] { xx.ToString(), yy.ToString(), zz });


            Corout.WhenAll(() => Routines.NStepVerbose(5),
                           () => Routines.NStepVerbose(10),
                           () => Routines.NStepVerbose(15))
            .OnSucceed(co =>
            {
                Debug.Log("OnSucceed");
            })
            .Start();


            Corout.WhenAll <int, float, string>(co => Routines.GetIntSquareRoot(x, co),
                                                co => Routines.GetFloatSquareRoot(y, co),
                                                co => Routines.GetFloatString(z, co))
            .OnSucceed(co =>
            {
                Debug.Log(co.Result.Item1);
                Debug.Log(co.Result.Item2);
                Debug.Log(co.Result.Item3);
            })
            .ContinueWith <string>(Routines.JoinString)
            .OnSucceed(co =>
            {
                Debug.Log(co.Result);
                Assert.AreEqual(cw, co.Result);
            })
            .Start();
        }