コード例 #1
0
        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);
            }
        }