Ejemplo n.º 1
0
 /// <summary>
 /// 停止定时器
 /// </summary>
 private void StopTimer()
 {
     if (timer != null && timer.Enabled)
     {
         timer.Stop();
     }
     //最后一次刷新容器
     if (ShowContanier != null)
     {
         if (!ShowContanier.IsDisposed && ShowContanier.Created)
         {
             ShowContanier.Invoke(new Action(() =>
             {
                 ShowContanier.Refresh();
             }));
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 定时器
        /// </summary>
        private void StartTimer()
        {
            StringFormat sf = new StringFormat()
            {
                Alignment     = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
            Font     f = new Font(ShowContanier.Font, FontStyle.Bold);
            Graphics g = ShowContanier.CreateGraphics();

            if (!ProgressTip.Contains("{0}"))
            {
                ProgressTip += "..用时{0}{1}";
            }

            if (timer == null)
            {
                timer          = new Timer(1000);
                timer.Elapsed += (s, e) =>
                {
                    if ((_progressForm != null && _progressForm.IsStop) || IsStop)
                    {
                        if (Worker != null)
                        {
                            try
                            {
                                if (WorkerThread != null && WorkerThread.IsAlive)
                                {
                                    StopTimer();
                                    WorkerThread.Abort();
                                }
                            }
                            catch (ThreadAbortException) { }
                            catch (Exception) { }
                        }
                    }

                    if (ShowContanier == null)
                    {
                        return;
                    }

                    //回调容器
                    ShowContanier.Invoke(new Action <DateTime>((datetime) =>
                    {
                        DateTime sTime = datetime;

                        //工作时间
                        _WorkerStartDateSecond++;

                        //...的个数
                        _ShowProgressCount++;

                        if (_ShowProgressCount > 6)
                        {
                            _ShowProgressCount = 1;
                        }

                        string[] strs = new string[_ShowProgressCount];

                        string ProgressStr = string.Join(".", strs);

                        string ProgressText = string.Format(ProgressTip, _WorkerStartDateSecond, ProgressStr);

                        if (_progressForm != null)
                        {
                            _progressForm.ReportProgress(_WorkerStartDateSecond, null);

                            _progressForm.TipMessage = ProgressText;
                            _progressForm.SetTip();
                        }
                        //必须在DrawString之前
                        ShowContanier.Refresh();

                        if (Mode == ShowMode.Text)
                        {
                            g.DrawString(ProgressText, f, ProgressBackColor, ShowContanier.ClientRectangle, sf);
                        }
                    }), e.SignalTime);
                };

                if (!timer.Enabled)
                {
                    timer.Start();
                }
            }
        }