Beispiel #1
0
        /// <summary>把用户工作项放入队列</summary>
        /// <param name="task">任务</param>
        /// <returns>任务编号</returns>
        private Int32 Queue(ThreadTask task)
        {
            //加锁,防止冲突
            lock (Sync_Tasks)
            {
                Tasks.Add(task.ID, task);

                //初始化线程池
                if (ManagerThread == null || !ManagerThread.IsAlive)
                {
                    Init();
                }
            }

            //通知管理线程,任务到达
            Event.Set();

            return(task.ID);
        }
Beispiel #2
0
        private void Work()
        {
            //Alive = true;
            while (true)
            {
                try
                {
                    //挂起自己,直到下一个任务到来
                    internalEvent.WaitOne(Timeout.Infinite, false);

                    Running = true;

                    //信号量复位
                    internalEvent.Reset();
                    if (Task != null)
                    {
                        ThreadTask task = Task;
                        WriteLog("新任务" + task.ID);
                        //task.Thread = this;
                        //WaitCallback method = Method;
                        //Object arg = Argument;
                        LastError = null;

                        StartTime = DateTime.Now;

                        //method(arg);
                        Task.Method(Task.Argument);
                    }
                }
                catch (ThreadInterruptedException ex)//中断异常,跳出
                {
                    LastError = ex;
                    Thread    = null;
                    internalEvent.Close();
                    internalEvent = null;
                    break;
                }
                catch (ThreadAbortException ex)//取消异常,有可能是终止当前任务而已,不需要跳出
                {
                    LastError = ex;

                    //异常参数指明是否需要终止线程
                    if (ex.ExceptionState != null && (Boolean)ex.ExceptionState)
                    {
                        Thread = null;
                        internalEvent.Close();
                        internalEvent = null;
                        break;
                    }
                }
                catch (Exception ex)//其它异常,继续
                {
                    LastError = ex;

                    ThreadAbortException e = FindException <ThreadAbortException>(ex);
                    //if (e == null)
                    //    XTrace.WriteException(ex);
                    //else
                    //{
                    //异常参数指明是否需要终止线程
                    if (e.ExceptionState != null && (Boolean)e.ExceptionState)
                    {
                        Thread = null;
                        internalEvent.Close();
                        internalEvent = null;
                        break;
                    }
                    //}
                }
                finally
                {
                    //通知事件订阅者,任务已经完成
                    if (_OnTaskFinished != null)
                    {
                        //对不信任方法的调用,捕获所有异常,防止因外部方法的错误而导致线程自身崩溃
                        try
                        {
                            _OnTaskFinished(this, EventArgs.Empty);
                        }
                        catch
                        {
                        }
                    }

                    //清空任务,防止下一次重复执行
                    Task = null;

                    AliveTime = DateTime.Now;

                    //不管怎么样,都要标志线程不再运行
                    Running = false;
                }
            }
            //Alive = false;
        }
Beispiel #3
0
        /// <summary>取消任务</summary>
        /// <param name="id">任务编号</param>
        /// <returns>任务状态</returns>
        public TaskState Abort(Int32 id)
        {
            // 重点:
            // 这里使用了锁,很危险,所以仅仅在锁里面删除任务,任务的善后处理在锁外面完成

            // 要取消的任务
            ThreadTask task = null;
            // 任务状态
            TaskState state = TaskState.Finished;

            #region 检查任务是否还在队列里面
            if (Tasks.ContainsKey(id))
            {
                //加锁,防止冲突
                lock (Sync_Tasks)
                {
                    if (Tasks.ContainsKey(id))
                    {
                        task = Tasks[id];

                        Tasks.Remove(id);
                        state = TaskState.Unstarted;
                    }
                }
            }
            #endregion

            #region 检查任务是否正在处理
            if (task == null && Threads.Count > 0)
            {
                lock (SyncLock_Threads)
                {
                    if (Threads.Count > 0)
                    {
                        foreach (ThreadX item in Threads)
                        {
                            if (item.Task != null && item.Task.ID == id)
                            {
                                task = item.Task;
                                Boolean b = item.Running;
                                item.Abort(true);
                                if (b)
                                {
                                    state = TaskState.Running;
                                }
                                else
                                {
                                    state = TaskState.Finished;
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            if (task == null)
            {
                state = TaskState.Finished;
            }

            // 处理任务结束时的事情
            if (task != null && task.AbortMethod != null)
            {
                try { task.AbortMethod(task.Argument); }
                catch { }
            }

            return(state);
        }