コード例 #1
0
ファイル: BatchTask.cs プロジェクト: renyh/dp2
        // 工作线程
        public virtual void ThreadMain()
        {
            try
            {
                WaitHandle[] events = new WaitHandle[2];

                events[0] = eventClose;
                events[1] = eventActive;

                while (true)
                {
                    int index = 0;
                    try
                    {
                        index = WaitHandle.WaitAny(events, PerTime, false);
                    }
                    catch (System.Threading.ThreadAbortException /*ex*/)
                    {
                        /*
                         * // 调试用
                         * LibraryApplication.WriteWindowsLog("BatchTask俘获了ThreadAbortException异常", EventLogEntryType.Information);
                         * */
                        this.App.Save(null, false);    // 触发保存
                        this.App.WriteErrorLog("刚才是ThreadAbortException触发了配置文件保存");
                        break;
                    }

                    if (index == WaitHandle.WaitTimeout)
                    {
                        // 超时
                        eventActive.Reset();
                        Worker();
                        eventActive.Reset();    // 2013/11/23 只让堵住的时候发挥作用
                    }
                    else if (index == 0)
                    {
                        break;
                    }
                    else
                    {
                        // 得到激活信号
                        eventActive.Reset();
                        Worker();
                        eventActive.Reset();    // 2013/11/23 只让堵住的时候发挥作用
                    }

                    // 是否循环?
                    if (this.Loop == false)
                    {
                        break;
                    }
                }
                this.ManualStart = false;   // 这个变量只在一轮处理中管用
            }
            catch (Exception ex)
            {
                string strErrorText = "BatchTask工作线程出现异常: " + ExceptionUtil.GetDebugText(ex);
                try
                {
                    this.App.WriteErrorLog(strErrorText);
                    this.AppendResultText(strErrorText + "\r\n");
                }
                catch
                {
                    LibraryApplication.WriteWindowsLog(strErrorText);
                }
            }
            finally
            {
                // 2009/7/16 移动到这里
                try
                {
                    eventFinished.Set();
                    eventStarted.Set();         // 2017/8/23
                }
                catch (ObjectDisposedException) // 2016/4/19
                {
                }

                // 2009/7/16 新增
                // this.m_bClosed = true;
                this.Stopped = true;
            }
        }