Example #1
0
        /// <summary>
        /// 开始新的任务
        /// </summary>
        /// <param name="item"></param>
        /// <param name="func"></param>
        /// <param name="taskFinishAction"></param>
        private void StartNewProcess(TaskJobParam <T> item, Func <T, CancellationTokenSource, T2> func,
                                     Action <bool, T2, Exception> taskFinishAction)
        {
            if (!OnGoingList.ContainsKey(item.Id))
            {
                OnGoingList.Add(item.Id, item.Data);
            }
            else
            {
                StartProcessForWait(func, taskFinishAction);
            }

            var cts   = new CancellationTokenSource();
            var token = cts.Token;
            var task  = Task.Factory.StartNew(() =>
                                              func.Invoke(item.Data, cts), token)
                        .ContinueWith((res) =>
            {
                try
                {
                    if (res == null)
                    {
                        taskFinishAction?.Invoke(false, default(T2), null);
                    }
                    else if (res.Exception != null)
                    {
                        taskFinishAction?.Invoke(false, ObjectCommon.SafeObject(res.Result != null, () =>
                                                                                ValueTuple.Create(res.Result, default(T2))),
                                                 res.Exception);
                    }
                    else
                    {
                        taskFinishAction?.Invoke(true,
                                                 ObjectCommon.SafeObject(res.Result != null, () =>
                                                                         ValueTuple.Create(res.Result, default(T2))),
                                                 null);
                    }
                }
                catch (Exception ex)
                {
                    // ignored
                }
                finally
                {
                    if (OnGoingList.ContainsKey(item.Id))
                    {
                        OnGoingList.Remove(item.Id);
                    }

                    StartProcessForWait(func, taskFinishAction);
                }
            }, token);
        }
Example #2
0
        /// <summary>
        /// 开始执行等待进行的任务
        /// </summary>
        private void StartProcessForWait(Func <T, CancellationTokenSource, T2> func,
                                         Action <bool, T2, Exception> taskFinishAction)
        {
            if (AwaitList.Count > 0 && IsStartNewProcess)
            {
                TaskJobParam <T> taskJobParam = base.GetFirstJob <TaskJobParam <T> >(AwaitList);
                if (taskJobParam != null)
                {
                    if (AwaitList.ContainsKey(taskJobParam.Id))
                    {
                        AwaitList.Remove(taskJobParam.Id);
                    }

                    StartNewProcess(taskJobParam, func, taskFinishAction);
                }
            }
            else
            {
                if (_duration > 0)
                {
                    Thread.Sleep(_duration);
                }
            }
        }