示例#1
0
        /// <summary>
        /// Runs the delegate asynchronously and returns the completion
        /// </summary>
        /// <exception cref="ActionBlockIsFullException">If the queue is full and the queue was configured to limit the queue size.</exception>
        public Task <T> RunAsync <T>(Func <Task <T> > runAsync)
        {
            var taskSource = TaskSourceSlim.Create <T>();

            m_actionBlock.Post(async() =>
            {
                try
                {
                    var task = runAsync();
                    taskSource.LinkToTask(task);
                    await task;
                }
                catch (Exception ex)
                {
                    // Still need to call TrySetException, because runAsync may fail synchronously.
                    taskSource.TrySetException(ex);
                }
            });

            return(taskSource.Task);
        }
示例#2
0
        /// <summary>
        /// Runs the delegate asynchronously and returns the completion
        /// </summary>
        public Task <T> RunAsync <T>(Func <Task <T> > runAsync)
        {
            var taskSource = TaskSourceSlim.Create <T>();

            m_actionBlock.Post(() =>
            {
                try
                {
                    var task = runAsync();
                    taskSource.LinkToTask(task);
                    return(task);
                }
                catch (Exception ex)
                {
                    taskSource.TrySetException(ex);
                    return(Task.CompletedTask);
                }
            });

            return(taskSource.Task);
        }