首页 新闻 会员 周边 捐助

调试时有错而运行正常

0
悬赏园豆:50 [已解决问题] 解决于 2017-04-10 14:30

以下这段代码我在Visual Studio中运行时会报错(线程间操作无效: 从不是创建控件“Form1”的线程访问它。)

为什么直接运行exe没有问题?

      private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("kaishi");
            BackgroundWorker BackGW = new BackgroundWorker();
            BackGW.DoWork += bwload_DoWork;
            BackGW.RunWorkerAsync();
        }
        private void bwload_DoWork(object sender, DoWorkEventArgs e)
        {
            Form2 jd = new Form2();
            FormCollection forms = Application.OpenForms;
            Form formTemp = forms[0];
            IWin32Window owner = formTemp;
            jd.ShowDialog(owner);
            MessageBox.Show("jieshu");       
        }
      private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("kaishi");
            BackgroundWorker BackGW = new BackgroundWorker();
            BackGW.DoWork += bwload_DoWork;
            BackGW.RunWorkerAsync();
        }
        private void bwload_DoWork(object sender, DoWorkEventArgs e)
        {
            Form2 jd = new Form2();
            FormCollection forms = Application.OpenForms;
            Form formTemp = forms[0];
            IWin32Window owner = formTemp;
            jd.ShowDialog(owner);
            MessageBox.Show("jieshu");        
        }
左眼水星的主页 左眼水星 | 初学一级 | 园豆:123
提问于:2017-03-30 14:23
< >
分享
最佳答案
0

为了程序的健壮可能异常被捕获了,虽然正常却有隐患。解决下为好

收获园豆:40
codingHeart | 小虾三级 |园豆:1511 | 2017-03-30 15:43
其他回答(2)
1

所有STA线程的数据还是乖乖用Dispatcher访问比较好

OpportunityLiu | 园豆:255 (菜鸟二级) | 2017-03-30 16:54
0

http://www.cnblogs.com/tom-tong/archive/2012/02/22/2363965.html

BackgroundWorker 不能这样用.

收获园豆:10
forhells | 园豆:724 (小虾三级) | 2017-04-01 12:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册