需求:在开发过程中经常需要部分数据等待的加载。可能因为某个表比较慢而卡到主线程,所以需要实现局部加载等待。(在Web肯定就ajax异步就好做,winform就比较复杂点了)

公司主营业务:成都网站设计、成都做网站、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出平邑免费做网站回馈大家。
效果图
效果图的主要效果没展示出来,就这样吧。懒了懒了!
用用户控件新建等待窗口
//Loding2的类 public partial class Loading2 : UserControl, IDisposable//继承IDisposable { Timer timer1 = new Timer(); public Loading2(Control contorl) { InitializeComponent(); this.Parent = contorl; this.Parent.Enabled = false; this.Visible = false;//默认不显示
timer1=new System.Windows.Forms.Timer(this.components);
timer1.Interval=30000;
timer1.Tick+=new System.EventHandler(this.timer1_Tick);
}
public void Close()
{
//自写Close方法释放资源
this.Dispose();
}
//开始运行计时以及显示
public void Start()
{
this.SendToBack();
this.Visible = true;//显示
timer1.Enabled = true;//开始计时
}
private void Loading2_Layout(object sender, LayoutEventArgs e)
{
ReLocation();//绘制位置到父级控件中间
}
private void ReLocation()
{
int x = (int)(0.5 * (Parent.Width - this.Width));
int y = (int)(0.5 * (Parent.Height - this.Height));
this.Location = new System.Drawing.Point(x, y);
}
private void timer1_Tick(object sender, EventArgs e)
{
LogHelper.Instance.Error("加载超时!");
timer1.Stop();//手动停止,不然就算用户控件Dispose了,timer也不会释放和停止
Close();//超时关闭
}
///
/// 使用new关键字覆盖原有System.Windows.Forms的Dispose
///
private new void Dispose()
{
if (this.InvokeRequired)//在线程中调用,使用Invoke执行是否该用户控件代码
{
this.Invoke(new Action(() =>
{
if (this.Parent != null)
{
this.Parent.Enabled = true;//启用父控件
}
Dispose(true);timer1.Dispose();//timer1是非托管资源需手动结束回收,避免任务过多导致内存问题程序奔溃
}));
}
else
{
if (this.Parent != null)
{
this.Parent.Enabled = true;
}
Dispose(true);timer1.Dispose();
}
}
}