如何删除'System.InvalidOperationException'? [英] How to remove 'System.InvalidOperationException'?

查看:70
本文介绍了如何删除'System.InvalidOperationException'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的代码后出现错误,然后显示它链接到每次迭代中的textBox更新。



when I am running my code following error appeard which then showed that it linked to the update of a textBox in each iteration.

epoch++;
// check the conditions to stop
if ((epochs != 0) && (epoch > epochs))
    break;

// set current epoch's info
textBox.Text = epoch.ToString();





System.Windows.Forms.dll中出现未处理的System.InvalidOperationException类型异常



调用线程无法访问此对象,因为另一个线程拥有它。



我该如何解决?



问候

AM



An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

The calling thread cannot access this object because a different thread owns it.

How can I resolve it?

regards
AM

推荐答案

简单,你正在使用更多线程。但是GUI线程无法从另一个线程更新。你需要一些调用:

easy, you are using more threads. But the GUI thread cannot be updated from another thread. You need some invoking:
textBox.Invoke(new Action(()=> textBox.Text = epoch.ToString()));



这应该可以解决问题!

更新来自不同线程的值时,所有GUI组件都需要INVOKE。



好​​运。


That should do the trick!
All you GUI components need INVOKE when updating the values from a different thread.

GOOD Luck.


这篇关于如何删除'System.InvalidOperationException'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆