示例#1
0
        public ITask Task(System.Func <object> task)
        {
            var taskRunner = new ThreadTask(this)
            {
                TaskWithResult = task,
                ReturnResult   = true,
            };

            return(taskRunner);
        }
示例#2
0
        public ITask Task(System.Action task)
        {
            var taskRunner = new ThreadTask(this)
            {
                Task         = task,
                ReturnResult = false,
            };

            return(taskRunner);
        }
示例#3
0
 public void Cancel(ThreadTask taskRunner)
 {
     taskRunnerLocker.EnterWriteLock();
     try
     {
         this.taskRunner.Remove(taskRunner);
     }finally
     {
         taskRunnerLocker.ExitWriteLock();
     }
 }
示例#4
0
        private void RunTaskThread(ThreadTask taskRunner)
        {
            try
            {
                object result = null;
                if (taskRunner.ReturnResult)
                {
                    result = taskRunner.TaskWithResult();
                }
                else
                {
                    taskRunner.Task();
                }

                if (taskRunner.Complete != null)
                {
                    App.MainThread(() =>
                    {
                        taskRunner.Complete();
                    });
                }

                if (taskRunner.CompleteWithResult != null)
                {
                    App.MainThread(() =>
                    {
                        taskRunner.CompleteWithResult(result);
                    });
                }
            }
            catch (System.Exception exception)
            {
                if (taskRunner.Error != null)
                {
                    App.MainThread(() =>
                    {
                        taskRunner.Error(exception);
                    });
                }
            }
        }
示例#5
0
 public ITaskHandler AddTask(ThreadTask taskRunner)
 {
     taskRunner.StartTime = App.Time.Time;
     if (taskRunner.DelayTime > 0)
     {
         taskRunnerLocker.EnterWriteLock();
         try
         {
             this.taskRunner.Add(taskRunner);
         }
         finally
         {
             taskRunnerLocker.ExitWriteLock();
         }
     }
     else
     {
         ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadExecuter), taskRunner);
     }
     return(taskRunner);
 }
示例#6
0
 /// <summary>
 /// 添加任务
 /// </summary>
 /// <param name="task">任务</param>
 /// <returns>任务句柄</returns>
 public ITaskHandler AddTask(ThreadTask task)
 {
     task.StartTime = Time.Time;
     if (task.DelayTime > 0)
     {
         taskRunnerLocker.EnterWriteLock();
         try
         {
             taskRunner.Add(task);
         }
         finally
         {
             taskRunnerLocker.ExitWriteLock();
         }
     }
     else
     {
         ThreadPool.QueueUserWorkItem(ThreadExecuter, task);
     }
     return(task);
 }