后台线程的处理

1.创建一个线程

private System.Threading.Thread _serviceThread = null;

 

2.初始化该线程(加载的时候初始化)

 _serviceThread = new System.Threading.Thread(new System.Threading.ThreadStart(GetServiceStatus));
 _serviceThread.IsBackground = true;
 _serviceThread.Start();

 

3后台线程处理方法

 public void GetServiceStatus()        

{

           bool isService = true;

           int count = 0;  

           while (true)          

          {

                     try  {            

                                if (this.IsDisposed || !this.IsHandleCreated) { return; }                            

                                 isService = CommonService.CheckConnection();                    

                                 if (!isService)  {    RefreshService(0, false);  }

                                 else               {       count = _sysService.GetNotReadAffiche                      (ClientConfig.Current.OwnerBranch.ChBranchno,Singleton<OperatorModel>.Instance.VchOperID);                      

                                                               RefreshService(count, isService);                    

                                                      }

                            }
                    catch (Exception ex)
                    {
                       if (!isService)
                        {
                        RefreshService(0, false);
                        }
                        else
                       {
                           RefreshService(0, true);
                       }
                    System.Threading.Thread.Sleep(10000);
                }

                     System.Threading.Thread.Sleep(10000);

}}

///4.刷新主线程

        public void RefreshService(int count, bool isService)
        {
            try
            {

                if (this.IsDisposed || !this.IsHandleCreated) { return; }
                if (this.InvokeRequired)
                {
                    this.Invoke(new Action(() => { RefreshService(count, isService); }));
                }
                else
                {

                    if (isService)
                    {
                        lblStatus.Text = "状态:联网";
                        lblStatus.ForeColor = Color.Green;
                        if (count > 0)
                        {
                            lblNotice.Text = string.Format("公告:{0}条", count.ToString());
                        }
                        else
                        {
                            lblNotice.Text = "";
                        }
                    }

                    else
                    {
                        lblStatus.Text = "状态:断网";
                        lblStatus.ForeColor = Color.Red;
                        lblNotice.Text = "";
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

 

//5.释放该线程

 this.FormClosing += (s, e) =>
            {
                if (_serviceThread != null)
                {
                    _serviceThread.Abort();
                }

};

 

posted @ 2017-04-05 09:37  武汉程序猿  阅读(88)  评论(0)    收藏  举报