How can I assign a name to a task in TPL

后端 未结 11 1273
礼貌的吻别
礼貌的吻别 2021-02-12 10:24

I\'m going to use lots of tasks running on my application. Each bunch of tasks is running for some reason. I would like to name these tasks so when I watch the Parallel Tasks wi

11条回答
  •  囚心锁ツ
    2021-02-12 11:14

    public class NamedTaskSchedular
    {
        private static readonly ConcurrentDictionary NamedTaskDictionary = new ConcurrentDictionary();
    
        public static Task RunNamedTask(string name, Action action)
        {
            if (NamedTaskDictionary.ContainsKey(name))
            {
                return NamedTaskDictionary[name].RunTask(action);
            }
            var task = new NamesTask();
    
            NamedTaskDictionary[name] = task;
    
            return task.RunTask(action);
        }
    }
    

提交回复
热议问题