zoukankan      html  css  js  c++  java
  • 经过指定的时间后自动关闭的模式窗体

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

    代码如下:

    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

  • 相关阅读:
    [Java]如何把当前时间插入到数据库
    [Java]Get与Post,客户端跳转与服务器端跳转
    [Java]MyBatis框架
    [Java]Java分层概念(转)
    [Java]Spring框架
    [Java]JavaScript在这里学习
    [Java]Servlet&JSP
    [Java]jdbc[转]
    [工具]GitHub上整理的一些工具[转]
    [Java] 集合框架
  • 原文地址:https://www.cnblogs.com/w-pound/p/15405651.html
Copyright © 2011-2022 走看看