以下这段代码我在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"); }
所有STA线程的数据还是乖乖用Dispatcher访问比较好
http://www.cnblogs.com/tom-tong/archive/2012/02/22/2363965.html
BackgroundWorker 不能这样用.