博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
封装一个进度条
阅读量:6543 次
发布时间:2019-06-24

本文共 4942 字,大约阅读时间需要 16 分钟。

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 : IProgress13     {14         private Thread m_Thread;15         public Thread mThread16         {17             get18             {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 IProgressView2     {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

你可能感兴趣的文章
LVS负载均衡LAMP平台
查看>>
wex5怎么配合做seo 优化
查看>>
华为存储行吗?之研发篇
查看>>
莫言老师最精彩的一段话:
查看>>
我的友情链接
查看>>
理解mouseover,mouseout,mouseenter,mouseleave
查看>>
我的友情链接
查看>>
SharePoint2013切换帐户登录菜单显示
查看>>
IT十八掌掌第十天课程总结
查看>>
[软件仓库]CentOS下配置yum本地源服务环境
查看>>
c++程序真正的入口函数
查看>>
Cacti邮件和阀值预警
查看>>
电脑进入bios和u盘启动快捷键
查看>>
ELK平台搭建 ES
查看>>
ini_set 为一个配置选项设置值
查看>>
U盘装机记录
查看>>
headfirst PMP-项目管理的5个过程组
查看>>
outlook2003中收到邮件中的图片无法显示處理方法
查看>>
Linux系统内存分配机制
查看>>
我的友情链接
查看>>