非常普遍的,当你用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);
}
}
调用方法:
using (new WaitIndicator())
{
//code to process}