Ejemplo n.º 1
0
        public async Task Post_When_Called_Outside_Strand_Action_Is_Posted()
        {
            var originalTaskId   = INVALID_TASK_ID;
            var inDispatchTaskId = INVALID_TASK_ID;

            Task innerTask = null;
            var  outerTask = Task.Run(() =>
            {
                originalTaskId = Task.CurrentId.Value;
                innerTask      = m_strandScheduler.Post(() => inDispatchTaskId = Task.CurrentId.Value);
            });

            await outerTask;
            await innerTask;

            Assert.AreNotEqual(INVALID_TASK_ID, originalTaskId);
            Assert.AreNotEqual(INVALID_TASK_ID, inDispatchTaskId);
            Assert.AreNotEqual(originalTaskId, inDispatchTaskId);
        }
        private void postTargetSub(StrandSchedulerDecorator strand, IInvocation invocation)
        {
            void postAction()
            {
                invocation.GetConcreteMethodInvocationTarget()
                .Invoke(invocation.InvocationTarget, invocation.Arguments);
                //Problems with Castle implementation (call from other thread does not work after upgrade to 4.3.1. version)
                //invocation.Proceed();
            }

            strand.Post((Action)postAction);
        }
        private void postTargetFunc(StrandSchedulerDecorator strand, IInvocation invocation)
        {
            var     methodInvocationTarget = invocation.MethodInvocationTarget;
            dynamic proxyTcs = null;

            proxyTcs = getProxyTcs(methodInvocationTarget);
            var isGenericReturnType = methodInvocationTarget.ReturnType.IsGenericType;

            invocation.ReturnValue = proxyTcs.Task;

            strand.Post(postFunc);

            Task postFunc()
            {
                Task resultTask   = null;
                var  hasException = false;

                try
                {
                    resultTask = invocation.GetConcreteMethodInvocationTarget()
                                 .Invoke(invocation.InvocationTarget, invocation.Arguments) as Task;
                    //Problems with Castle implementation (call from other thread does not work after upgrade to 4.3.1. version)
                    //invocation.Proceed();
                }
                catch (Exception e)
                {
                    hasException = true;
                    if (resultTask == null)
                    {
                        resultTask = TaskEx.TaskFromException(e);
                    }
                }
                finally
                {
                    if (!hasException && isGenericReturnType)
                    {
                        TaskEx.PrepareTcsTaskFromExistingTask((dynamic)resultTask, proxyTcs);
                    }
                    else
                    {
                        TaskEx.PrepareTcsTaskFromExistingTask(resultTask, proxyTcs);
                    }
                }

                return(resultTask);
            }
        }