您的位置:首页 > 其它

经过指定的时间后自动关闭的模式窗体

2012-07-16 19:54 232 查看
在开发单线图排版算法演示功能时,需要每执行一步排版过程,调整了电力设备的位置后就暂停一下,笔者第一个想到的方法就是让主线程暂停

代码如下:

private void showlayout_delay(double p_second)
{
DateTime now = DateTime.Now;
while (now.AddSeconds(p_second) > DateTime.Now)
{
}
return;
}



System.Threading.Thread.Sleep(p_waitMilliSecond);

上面的方法虽然让程序暂停下来,但是却存在一个问题:无论排版过程运行了多少步,程序的图形状态始终和初始时一样,不会变化

即使使用Invalidate、refresh方法强制刷新整个屏幕

最终使了个小聪明解决了该问题:制作一个经过指定的时间后自动关闭的模式窗体

代码如下

public partial class FormDialogAutoClose : Form
{
private Thread waitThread;
public FormDialogAutoClose(int p_waitMilliSecond)
{
InitializeComponent();
waitThread = new Thread(new ThreadStart(delegate()
{
System.Threading.Thread.Sleep(p_waitMilliSecond);
this.closeByThread();
waitThread.Abort();
}));
waitThread.Start();

}
private delegate void closeByThreadHandler();
public void closeByThread()
{
if (this.InvokeRequired)
{
this.Invoke(new closeByThreadHandler(closeByThread));
}
else
{
this.Close();
}
}
}

调用代码

FormDialogAutoClose t_formClose = new FormDialogAutoClose(暂停时间);
t_formClose.ShowDialog();

同时为了“掩人耳目”,将该模式窗体的Visible属性设置为false
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐