| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 663 人关注过本帖
标题:一个后台处理的类
只看楼主 加入收藏
冷星
Rank: 1
等 级:新手上路
帖 子:67
专家分:9
注 册:2007-12-13
结帖率:66.67%
收藏
 问题点数:0 回复次数:0 
一个后台处理的类
//模仿BackgroudWorker写的类
//实现了一部分比较实用的功能
//这个类仅仅用正则表达式匹配大文本,并返回匹配结果
//对处的事件有二个:
//JobComplete和JobFailed
//如果需要做其它工作可在DoJob()过程里修改

    public delegate void JobCompleteHandle(System.Text.RegularExpressions.MatchCollection e);
    public delegate void JobFailHandle(string e);
    public delegate void StatusChangedHandle(bool e);

    public class BGWorker
    {
        public event JobCompleteHandle JobComplete;
        public event JobFailHandle JobFailed;

        private event JobCompleteHandle JobCompletePretreat;
        private event JobFailHandle JobFailedPretreat;
        private event StatusChangedHandle StatusChanged;

        string s_ToTreat = string.Empty;
        string s_RegularString = string.Empty;
        System.Text.RegularExpressions.RegexOptions ro_Option = System.Text.RegularExpressions.RegexOptions.Multiline;

        bool blIsBusy = false;

        System.Windows.Forms.Form frmOwner;
        System.Threading.Thread thrdWorker;

//传递进来的Form主要作用就是使子线程能从主线引发JobComplete和JobFailed事件
        public BGWorker(System.Windows.Forms.Form frmOwner)
        {
            this.frmOwner = frmOwner;
            thrdWorker = CreateThread();
            this.JobCompletePretreat += new JobCompleteHandle(BGWorker_JobCompletePretreat);
            this.JobFailedPretreat += new JobFailHandle(BGWorker_JobFailedPretreat);
            this.StatusChanged += new StatusChangedHandle(BGWorker_StatusChanged);
        }

//切换线程
        void BGWorker_JobFailedPretreat(string e)
        {
            if (frmOwner.InvokeRequired)
            {
                JobFailHandle jfhT = new JobFailHandle(BGWorker_JobFailedPretreat);
                frmOwner.Invoke(jfhT, new object[] { e });
            }
            else
            {
                JobFailed(e);
            }
        }

//切换线程,如果没有的话,从外部取得的IsBusy将保持为False
        void BGWorker_StatusChanged(bool e)
        {
            if (frmOwner.InvokeRequired)
            {
                StatusChangedHandle schT = new StatusChangedHandle(BGWorker_StatusChanged);
                frmOwner.Invoke(schT, new object[] { e });
            }
            else
            {
                blIsBusy = e;
            }
        }

//切换线程
        void BGWorker_JobCompletePretreat(System.Text.RegularExpressions.MatchCollection e)
        {
            if (frmOwner.InvokeRequired)
            {
                JobCompleteHandle jchT = new JobCompleteHandle(BGWorker_JobCompletePretreat);
                frmOwner.Invoke(jchT, new object[] { e });
            }
            else
            {
                JobComplete(e);
            }
        }

        public string sToTreat
        {
            set
            {
                s_ToTreat = value;
            }
        }

        public string sRegularString
        {
            set
            {
                s_RegularString = value;
            }
        }

        public System.Text.RegularExpressions.RegexOptions roOption
        {
            set
            {
                ro_Option = value;
            }
        }

        public bool IsReady
        {
            get
            {
                return s_RegularString.Length > 0 && s_ToTreat.Length > 0;
            }
        }

        public bool IsBusy
        {
            get
            {
                return blIsBusy;
            }
        }

        public void StartJob()
        {
            if (thrdWorker.ThreadState != System.Threading.ThreadState.Unstarted)
            {
                thrdWorker.Abort();//做比较复杂的工作不建议直接Abort,这样容易导致数据丢失,但在这里面不怕丢失所以直接Abort
                thrdWorker = CreateThread();
            }
            thrdWorker.Start();
        }

        public void StopJob()
        {
            if (thrdWorker.ThreadState == System.Threading.ThreadState.Running)
            {
                thrdWorker.Abort();
                blIsBusy = false;
            }
        }

        private System.Threading.Thread CreateThread()
        {
            System.Threading.Thread thrT = new System.Threading.Thread(new System.Threading.ThreadStart(DoJob));
            thrT.Name = "WorkThread";
            thrT.Priority = System.Threading.ThreadPriority.Lowest;
            thrT.IsBackground = true;
            return thrT;
        }

        void DoJob()
        {
            StatusChanged(true);
            StringBuilder sbT = new StringBuilder();
            try
            {
                System.Text.RegularExpressions.Regex reT = new System.Text.RegularExpressions.Regex(s_RegularString, ro_Option);
                System.Text.RegularExpressions.MatchCollection mcT = reT.Matches(s_ToTreat);
                JobCompletePretreat(mcT);
            }
            catch (Exception E)
            {
                JobFailedPretreat(E.Message);
            }
            StatusChanged(false);
        }
    }
搜索更多相关主题的帖子: 后台 
2008-03-22 20:13
快速回复:一个后台处理的类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020086 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved