123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using System;
- using System.Drawing;
- using System.Threading;
- using System.Windows.Forms;
- namespace UAS_MES_NEW.CustomControl
- {
- public partial class ProcessBar : UserControl
- {
- public delegate void TaskHandler(ref float percentage);
- private Thread _monitorThread = null;
- private Thread _workerThread = null;
- private float _percentage = 0f;
- private ProcessBarStatue _currentStatue = ProcessBarStatue.UnStarted;
- private TaskHandler _task = null;
- public ProcessBar()
- {
- InitializeComponent();
- this._monitorThread = new Thread(new ThreadStart(Monitor));
- }
-
-
-
-
- public TaskHandler Task
- {
- set
- {
-
- this._workerThread = new Thread(() => value(ref _percentage));
- this._task = value;
- }
- }
-
-
-
- public float Percentage
- {
- get
- {
- return this._percentage;
- }
- }
-
-
-
- public ProcessBarStatue CurrentStatue
- {
- get
- {
- return this._currentStatue;
- }
- }
- public void Run()
- {
- if (this._workerThread == null)
- {
- throw new NullReferenceException("Task委托不能为空!");
- }
-
- if (this._currentStatue == ProcessBarStatue.UnStarted)
- {
- _monitorThread.IsBackground = true;
- _monitorThread.Start();
- _workerThread.IsBackground = true;
- _workerThread.Start();
- this._currentStatue = ProcessBarStatue.Running;
- }
-
- else if (this._currentStatue == ProcessBarStatue.Aborted)
- {
- this._currentStatue = ProcessBarStatue.Running;
-
- if (this._workerThread.ThreadState == ThreadState.Aborted)
- {
- this._workerThread = null;
- this._workerThread = new Thread(() => _task(ref _percentage));
- }
- _workerThread.IsBackground = true;
- _workerThread.Start();
- }
- else
- {
- throw new InvalidOperationException("已经开始的任务无法再次开始!");
- }
- }
- public void Stop()
- {
- if (_workerThread == null)
- {
- throw new NullReferenceException("Task委托不能为空!");
- }
- if (this._currentStatue == ProcessBarStatue.Aborted)
- {
- throw new InvalidOperationException("已经终止的操作无法暂停!");
- }
- if (this._currentStatue != ProcessBarStatue.Suspended)
- {
-
-
- this._currentStatue = ProcessBarStatue.Suspended;
- }
- }
- public void Resume()
- {
- if (_workerThread == null)
- {
- throw new NullReferenceException("Task委托不能为空!");
- }
- if (this._currentStatue == ProcessBarStatue.Aborted)
- {
- throw new InvalidOperationException("已经终止的操作无法继续!");
- }
- if (this._currentStatue == ProcessBarStatue.Suspended)
- {
-
-
- this._currentStatue = ProcessBarStatue.Running;
- }
- }
- public void Abort()
- {
- if (this._workerThread == null)
- {
- throw new NullReferenceException("Task委托不能为空!");
- }
- if (this._currentStatue != ProcessBarStatue.Aborted)
- {
-
- this.Resume();
- _workerThread.Abort();
- this._currentStatue = ProcessBarStatue.Aborted;
- }
- }
- private void Monitor()
- {
-
- while (true)
- {
-
- if (this._currentStatue == ProcessBarStatue.Running)
- {
-
- while (this._percentage <= 1f)
- {
-
- using (var graphic = pictureBox1.CreateGraphics())
- {
- if (this.pictureBox1.InvokeRequired)
- {
- this.Invoke(new Action(() => Draw(this._percentage, graphic)));
- }
- else
- {
- this.Draw(this._percentage, graphic);
- }
- Thread.Sleep(10);
-
- if (this._currentStatue == ProcessBarStatue.Aborted)
- {
- this._percentage = 0f;
- if (this.pictureBox1.InvokeRequired)
- {
- this.Invoke(new Action(() => this.Clear(graphic)));
- }
- else
- {
- this.Clear(graphic);
- }
- break;
- }
- }
- }
- }
- }
- }
-
-
-
-
-
- protected virtual void Draw(float percentage, Graphics graphic)
- {
- graphic.FillRectangle(Brushes.Black, 0f, 0f, _percentage * pictureBox1.Width, pictureBox1.Height);
- }
-
-
-
-
- private void Clear(Graphics graphic)
- {
- graphic.Clear(Color.White);
- }
- public enum ProcessBarStatue
- {
- UnStarted,
- Running,
- Suspended,
- Aborted
- }
- }
- }
|