封装一个进度条

1.首先定义一个处理数据需要产生进度的接口和抽象类

 1 namespace Progress.Core
 2 {
 3     public interface IProgress
 4     {
 5         IProgressView m_FrmProgress { get; set; }
 6         PosscessCompleted mPosscessCompleted { get; set; }
 7         Thread mThread { get; }
 8         bool IsStop { get; set; }
 9         void Start();
10     }
11     public delegate void PosscessCompleted();
12     public class AbstractProgress : IProgress
13     {
14         private Thread m_Thread;
15         public Thread mThread
16         {
17             get
18             {
19                 return m_Thread;
20             }
21         }
22         public IProgressView m_FrmProgress { get; set; }
23         public bool IsStop { get; set; }
24 
25 
26         public PosscessCompleted mPosscessCompleted { get; set; }
27 
28         public AbstractProgress()
29         {
30             m_Thread = new Thread(new ThreadStart(WorkFunc));
31         }
32         bool isStart = false;
33         public void Start()
34         {
35             if (mThread != null && isStart == false)
36             {
37                 mThread.Start();
38                 isStart = true;
39             }
40         }
41         private void WorkFunc()
42         {
43             if (IsStop)
44             {
45                 return;
46             }
47             ProcessData();
48             //处理完成
49             if (mPosscessCompleted != null)
50             {
51                 mPosscessCompleted();
52             }
53         }
54         public virtual void ProcessData()
55         {
56             // mFrmProgress.IsFalse = true;
57         }
58     }
59 
60 }
IProgress

看一个子类实现:

 1 class NumCul : AbstractProgress
 2     {
 3         public override void ProcessData()
 4         {
 5             int i = 0;
 6             while (true)
 7             {
 8                 if (IsStop)
 9                 {
10                     break;
11                 }
12                 i++;
13                 m_FrmProgress.SetTitle(i.ToString());
14                 m_FrmProgress.SetProcessBar(i / 100);
15                 if (i > 10000)
16                 {
17                     break;
18                 }
19             }
20         }
21     }
NumCul

2.进度条窗体的抽象接口:

1  public interface IProgressView
2     {
3         void SetTitle(string title);
4 
5         void SetProcessBar(int percent);
6     }
IProgressView

一个进度条窗体实现:

 1  public partial class FrmProgress : Form, IProgressView
 2     {
 3         private IProgress mProgress = null;
 4         //处理进度条
 5         public delegate void ProgressBarState(int per);
 6         public ProgressBarState mProgressPorBar;
 7         //处理文字说明
 8         public delegate void TitleState(string title);
 9         public TitleState mProgressTitle;
10 
11         //处理按钮状态
12         public delegate void ButtonState();
13         public ButtonState mButtonState;
14 
15         public FrmProgress(IProgress progress)
16         {
17             InitializeComponent();
18             mProgress = progress;
19             mProgress.m_FrmProgress = this;
20         }
21 
22         private void FrmProgress_Load(object sender, EventArgs e)
23         {
24             mProgressPorBar += new ProgressBarState(SetProgressBarState);
25             mProgressTitle += new TitleState(SetTitleState);
26             mButtonState += new ButtonState(SetButtonState);
27             mProgress.mPosscessCompleted += new PosscessCompleted(IsCompeted);
28             mProgress.Start();
29         }
30         public bool IsFalse = true;
31         //给处理线程调用的
32         public void SetTitle(string title)
33         {
34             label1.Invoke(mProgressTitle, title);
35 
36         }
37         //给处理线程调用的
38         public void SetProcessBar(int percent)
39         {
40             progressBar1.Invoke(mProgressPorBar, percent);
41         }
42         public void IsCompeted()
43         {
44             this.button1.Invoke(mButtonState);
45         }
46 
47         void SetTitleState(string title)
48         {
49             label1.Text = title;
50         }
51         void SetProgressBarState(int percent)
52         {
53             progressBar1.Value = percent;
54         }
55         void SetButtonState()
56         {
57             Thread.Sleep(100);
58             button1.Text = "关闭";
59         }
60 
61         private void button1_Click(object sender, EventArgs e)
62         {
63             if (mProgress != null)
64             {
65                 mProgress.IsStop = true;
66             }
67             this.Close();
68         }
69 
70         private void FrmProgress_FormClosing(object sender, FormClosingEventArgs e)
71         {
72             if (mProgress != null)
73             {
74                 mProgress.IsStop = true;
75             }
76         }
77       
78     }
进度条窗体

3.调用:

1  private void button2_Click(object sender, EventArgs e)
2         {
3             IProgress pros = new NumCul();
4             Core.FrmProgress frm = new Core.FrmProgress(pros);
5             frm.ShowDialog();
6         }
View Code

 

原文地址:https://www.cnblogs.com/yhlx125/p/3660321.html