コード例 #1
0
        /// <summary>
        /// 获取待执行任务
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="function"></param>
        /// <returns></returns>
        private AwaitableTask <TResult> GetExecutableTask <TResult>(Func <TResult> function)
        {
            var awaitableTask = new AwaitableTask <TResult>(new Task <TResult>(function), Guid.NewGuid().ToString());

            SetLastTask(awaitableTask);
            return(awaitableTask);
        }
コード例 #2
0
        /// <summary>
        /// 获取待执行任务
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="function"></param>
        /// <returns></returns>
        private AwaitableTask <TResult> GetExecutableTask <TResult>(Func <TResult> function)
        {
            var awaitableTask = new AwaitableTask <TResult>(new Task <TResult>(function));

            AddPenddingTaskToQueue(awaitableTask);
            return(awaitableTask);
        }
コード例 #3
0
        /// <summary>
        /// 获取待执行任务
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        private AwaitableTask GetExecutableTask(Action action)
        {
            var awaitableTask = new AwaitableTask(new Task(action), Guid.NewGuid().ToString());

            SetLastTask(awaitableTask);
            return(awaitableTask);
        }
コード例 #4
0
        /// <summary>
        /// 获取待执行任务
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        private AwaitableTask GetExecutableTask(Action action)
        {
            var awaitableTask = new AwaitableTask(new Task(action));

            AddPenddingTaskToQueue(awaitableTask);
            return(awaitableTask);
        }
コード例 #5
0
 /// <summary>
 /// 设置最后一个task
 /// </summary>
 /// <param name="task"></param>
 /// <returns></returns>
 private void SetLastTask(AwaitableTask task)
 {
     //添加队列,加锁。
     lock (_lockObject)
     {
         _lastTask = task;
     }
 }
コード例 #6
0
 /// <summary>
 /// 添加待执行任务到队列
 /// </summary>
 /// <param name="task"></param>
 /// <returns></returns>
 private void AddPenddingTaskToQueue(AwaitableTask task)
 {
     //添加队列,加锁。
     lock (_queue)
     {
         _queue.Enqueue(task);
         //开始执行任务
         _autoResetEvent.Set();
     }
 }
コード例 #7
0
 private bool TryGetNextTask(out AwaitableTask task)
 {
     task = null;
     while (_queue.Count > 0)
     {
         //获取并从队列中移除任务
         if (_queue.TryDequeue(out task) && (!AutoCancelPreviousTask || _queue.Count == 0))
         {
             //设置进行中的异步操作无效
             _lastDoingTask?.MarkTaskInvalid();
             _lastDoingTask = task;
             return(true);
         }
         //并发操作,设置任务不可执行
         task.SetNotExecutable();
     }
     return(false);
 }
コード例 #8
0
 /// <summary>
 /// 任务等待器
 /// </summary>
 /// <param name="awaitableTask"></param>
 public TaskAwaiter(AwaitableTask awaitableTask) => _awaitableTask = awaitableTask;