如何异步更新winform? [英] How can I update a winform asynchronously?

查看:273
本文介绍了如何异步更新winform?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个winform,和一个蓝牙连接与lego nxt砖。现在我想要每秒更新一次表格以读取传感器或电池电量。但是如果我为此启动了一个新线程,那么当线程想要设置标签文本时,会出现一个无效的线程操作异常。



任何人都可以帮我吗?

解决方案

您只能从创建的线程更新标签,以及您尝试从工作线程更新标签的外观。



查看方法 Control.BeginInvoke()并从那里拿走。还有其他方法,如 Control.EndInvoke Control.Invoke 以及属性 Control.InvokeRequired ,这也将在此上下文中有所帮助。


I've got a winform, and a bluetooth connection with a lego nxt brick. Now I want to update the form every second to read sensors or the battery level. But if I start a new thread for that, there i a invalid thread operation exception when the thread wants to set the label text.

Can anyone help me?

解决方案

You can update the label only from the thread that created it and by the looks of it you are trying to update it from the worker thread.

Have a look at the method Control.BeginInvoke() and take it from there. There are other methods like Control.EndInvoke and Control.Invoke and also the property Control.InvokeRequired, that will be helpful in this context as well.

这篇关于如何异步更新winform?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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