非常普遍的,当你用winform 开发时,我们的执行过程要花费一定量的时间,例如:当你单击查询时,执行一定的业务逻辑时,或者从数据库删除实体记录时。
这个类可以加入到你的界面类中帮助你显示进度条:
public class WaitIndicator : IDisposable { ProgressForm progressForm; Thread thread; bool disposed = false; //to avoid redundant call public WaitIndicator() { progressForm = new ProgressForm(); thread = new Thread(_ => progressForm.ShowDialog()); thread.Start(); } public void Dispose() { Dispose(true); } protected virtual void Dispose(bool disposing) { if (!disposed) { thread.Abort(); progressForm = null; } disposed = true; } } class ProgressForm : Form { public ProgressForm() { ControlBox = false; ShowInTaskbar = false; StartPosition = FormStartPosition.CenterScreen; TopMost = true; FormBorderStyle = FormBorderStyle.None; var progreassBar = new ProgressBar() { Style = ProgressBarStyle.Marquee, Size = new System.Drawing.Size(200, 20), ForeColor = Color.Orange, MarqueeAnimationSpeed = 40 }; Size = progreassBar.Size; Controls.Add(progreassBar); } }
【等待较长长时间的业务操作用到的Progressbar】
调用方法:
using (new WaitIndicator()) { //code to process}
推荐阅读
- c#用法技巧|c# treeView 绑定集合
- c#用法技巧|c# winform 通过编程取消事件(event)的注册
- C# 把Keys 直接转换为键盘上得Char值
- 简单方法hashtable持久化到xml文件中
- 通过代码方式操作IIS7