Example #1
0
        public void TestWorkstepAsyncExecution()
        {
            // defines and executes a workstep synchronously and asynchronously
            var s1A = new SequentialSubstep1();

            s1A.Initialise(_workContext);

            // synchronous execution
            var t1 = new T1 {
                a = 31
            };
            T2 t2A = s1A.Execute(t1).Result;

            Assert.AreEqual(31, t2A.b);

            // asynchronous execution
            // note: there are 3 ways to complete an async operation
            // 1. wait method
            t1.a = 42;
            AsyncResult <WorkflowOutput <T2> > ar1 = s1A.BeginExecute(t1, null);
            T2 t2B1 = ar1.EndInvoke().Result;

            Assert.AreEqual(42, t2B1.b);

            // 2. poll method
            t1.a = 53;
            AsyncResult <WorkflowOutput <T2> > ar2 = s1A.BeginExecute(t1, null);

            while (!ar2.IsCompleted)
            {
                Thread.Sleep(100);
            }
            T2 t2B2 = ar2.EndInvoke().Result;

            Assert.AreEqual(53, t2B2.b);

            // 3. callback method
            t1.a = 64;
            bool ar3Callback  = false;
            T2   t2B3; t2B3.b = 0;

            s1A.BeginExecute(
                t1,
                delegate(IAsyncResult ar)
            {
                ar3Callback = true;
                var ar3Temp = (AsyncResult <WorkflowOutput <T2> >)ar;
                t2B3        = ar3Temp.EndInvoke().Result;
            });
            while (!ar3Callback)
            {
                Thread.Sleep(100);
            }
            Assert.AreEqual(64, t2B3.b);
        }