Ejemplo n.º 1
0
        /// <summary>
        /// 开始任务
        /// </summary>
        /// <param name="taskId">任务id</param>
        /// <returns>是否成功添加</returns>
        public static bool Start(int taskId)
        {
            Task task;

            if (tasks.TryGetValue(taskId, out task))
            {
                if (task.Status == TaskStatus.Start)
                {
                    return(true);
                }

                // 开启线程执行任务
                Console.WriteLine("开启线程执行任务" + taskId);
                TaskWorker taskWorker = new TaskWorker(taskId, task);
                return(ThreadPool.QueueUserWorkItem(new WaitCallback(taskWorker.Start)));
            }
            else
            {
                // 抛出错误
                return(false);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 开始任务
 /// </summary>
 /// <param name="worker">任务worker</param>
 /// <returns>是否成功添加</returns>
 public static bool Start(TaskWorker worker)
 {
     return(ThreadPool.QueueUserWorkItem(new WaitCallback(worker.Start)));
 }