Exemple #1
0
 /// <summary>
 /// 移除任务结果值
 /// </summary>
 /// <param name="taskName"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static bool TryRemoveValue(string taskName, out ConcurrentResult result)
 {
     if (!ConcurrentResults.TryRemove(taskName, out result))
     {
         return(false);
     }
     result.StopExpiryTimer();
     return(true);
 }
Exemple #2
0
        /// <summary>
        /// 并发处理任务(不阻塞请求线程)
        /// </summary>
        /// <param name="taskName">任务名称</param>
        /// <param name="tasks">并发任务集合</param>
        /// <param name="concurrentOptions">并发项</param>
        /// <returns></returns>
        public static async Task ProcessingAsync(string taskName,
                                                 List <Func <Task> > tasks,
                                                 ConcurrentOptions concurrentOptions = default)
        {
            if (string.IsNullOrEmpty(taskName))
            {
                throw new ArgumentNullException(nameof(taskName));
            }

            tasks ??= new List <Func <Task> >();
            tasks = tasks.Where(taskFunc => taskFunc != null).ToList();
            if (tasks.Count <= 0)
            {
                return;
            }

            await Task.Yield();

            concurrentOptions ??= new ConcurrentOptions();

            var concurrentResultNew = new ConcurrentResult(taskName, tasks.Count, concurrentOptions.LifeTime);

            TryRemoveValue(taskName, out _);
            TryAddValue(taskName, concurrentResultNew);

            var semaphoreSlim = new SemaphoreSlim(concurrentOptions.MaxConcurrentCount);
            var taskList      = new List <Task>();

            foreach (var taskItem in tasks)
            {
                await semaphoreSlim.WaitAsync(concurrentOptions.SecondsTimeout * 1000);

                var task = taskItem()
                           .ContinueWith(t =>
                {
                    semaphoreSlim.Release();
                });

                taskList.Add(task);
                concurrentResultNew.ExecuteTaskList = taskList;
            }
            await Task.WhenAll(taskList);

            concurrentResultNew.Completed = true;
            concurrentResultNew.StartExpiryTimer(ExpiryTimer_Tick);
        }
Exemple #3
0
 /// <summary>
 /// 添加任务结果值
 /// </summary>
 /// <param name="taskName"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 internal static bool TryAddValue(string taskName, ConcurrentResult result)
 {
     return(ConcurrentResults.TryAdd(taskName, result));
 }
Exemple #4
0
 /// <summary>
 /// 获取任务结果值
 /// </summary>
 /// <param name="taskName"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static bool TryGetValue(string taskName, out ConcurrentResult result)
 {
     return(ConcurrentResults.TryGetValue(taskName, out result));
 }