internal static Task <TReturnType> CreateTaskDispatcher <TReturnType>(out Guid taskIdentity)
        {
            taskIdentity = Guid.NewGuid();

            var taskDispatch = new TaskDispatch();

            _taskDispatcher.Add(taskIdentity, taskDispatch);

            taskDispatch.CancelTokenSource = new CancellationTokenSource();
            taskDispatch.CancelToken       = taskDispatch.CancelTokenSource.Token;

            var taskAction = new Task <TReturnType>(() =>
            {
                //Will throw here if task faulted
                taskDispatch.ThrowExceptionIfFaulted();

                if (taskDispatch.ResultData == null || !taskDispatch.ResultData.TaskSuccess)
                {
                    return(default(TReturnType));
                }

                //In case of a void call, we must return the IgnoredType for the caller instead of TaskComplete
                if (typeof(TReturnType) == typeof(IgnoredType))
                {
                    taskDispatch.ResultData.ReturnValue = new IgnoredType();
                }

                return((TReturnType)taskDispatch.ResultData.ReturnValue);
            }, taskDispatch.CancelToken);

            taskDispatch.TaskId       = taskIdentity;
            taskDispatch.ResultAction = taskAction;

            return(taskAction);
        }
Esempio n. 2
0
        internal static Task <TReturnType> CreateTaskDispatcher <TReturnType>(out int taskIdentity)
        {
            taskQueueId++;
            taskIdentity = taskQueueId;

            var taskDispatch = new TaskDispatch();

            _taskDispatcher.Add(taskIdentity, taskDispatch);

            taskDispatch.CancelTokenSource = new CancellationTokenSource();
            taskDispatch.CancelToken       = taskDispatch.CancelTokenSource.Token;

            var taskAction = new Task <TReturnType>(() =>
            {
                //Will throw here if task faulted
                taskDispatch.ThrowExceptionIfFaulted();

                if (taskDispatch.ResultData == null || !taskDispatch.ResultData.TaskSuccess)
                {
                    return(default(TReturnType));
                }

                return((TReturnType)taskDispatch.ResultData.ReturnValue);
            }, taskDispatch.CancelToken);

            taskDispatch.TaskId       = taskIdentity;
            taskDispatch.ResultAction = taskAction;

            return(taskAction);
        }