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

  • 相关阅读:
    【动态规划】01背包问题
    【Huffman&&贪心】Fence Repair(POJ 3253)
    【STL学习】priority_queue
    【贪心算法】特殊的密码锁(openjudge8469)
    【贪心+二分】疯牛
    用类模板封装链表
    Qt的QString和C++string之间的转换
    Qt模态对话框和非模态对话框
    常见的交换变量的三种方法
    整理的经典面试题及各种库函数的自己实现
  • 原文地址:https://www.cnblogs.com/w-pound/p/15405651.html
Copyright © 2011-2022 走看看