zoukankan      html  css  js  c++  java
  • Form.ShowDialog(this)

    有时遇到一种情况,.ShowDialog()不显示。也不报错。例如以下:

    <span style="font-size:14px;"> private void button1_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(show);
                thread.Start();
            }
           void show()
           {
               Control.CheckForIllegalCrossThreadCalls = false;
               //this.Invoke(new Action(() =>
               //{
                   if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                   { }
               //}));
           }</span>
    原因分析:这属于线程间操作的一种异常。界面呈现和新创建的thread分开在两个线程中。在thread线程中

                      不可以进行界面呈现,即显示.ShowDialog();

    解决方法:1:加入參数this。

                            .ShowDialog(IWin32Window owner);    //owner:表示将拥有模式对话框的顶级窗体

    <span style="font-size:14px;"> private void button1_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(show);
                thread.Start();
            }
           void show()
           {
               Control.CheckForIllegalCrossThreadCalls = false;
               //this.Invoke(new Action(() =>
               //{
                   if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                   { }
               //}));
           }</span>

                      2:使用Invoke

            private void button1_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(show);
                thread.Start();
            }
           void show()
           {
               // Control.CheckForIllegalCrossThreadCalls = false;
               this.Invoke(new Action(() =>
               {
                   if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                   { }
               }));
           }

  • 相关阅读:
    php设计模式-适配器
    遍历Map的4种方法
    遍历数组
    遍历List的方法
    复选框选中
    单选框选中
    正向代理和反向代理
    对于Dubbo的理解
    python远程控制Linux
    python对中文的处理
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6753447.html
Copyright © 2011-2022 走看看