设为首页 加入收藏

TOP

Form.ShowDialog(this)
2015-11-21 01:00:12 来源: 作者: 【 】 浏览:2
Tags:Form.ShowDialog this

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

?

 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)
               { }
           //}));
       }
原因分析:这属于线程间操作的一种异常。界面呈现和新创建的thread分开在两个线程中。在thread线程中

?

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

解决方法:1:添加参数this。

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

?

 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)
               { }
           //}));
       }

?

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)
               { }
           }));
       }

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 3696 The Luckiest number 欧.. 下一篇[C++]对字符串向量排序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: