我怎么能异步更新一个WinForm? [英] How can I update a winform asynchronously?

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

问题描述

我有一个WinForm,并用乐高NXT砖蓝牙连接。现在我想以更新的形式每秒钟读取传感器或电池电量。但是,如果我开始一个新的线程如此,还有我当线程要设置标签文本一个无效的线程运行异常。

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.

谁能帮我?

推荐答案

您只能从创建它的线程和它的外观,你正试图从工作者线程更新更新的标签。

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.

看一看方法 Control.BeginInvoke(),并从那里。还有其他的方法,如 Control.EndInvoke 并的Control.Invoke 也是财产的 Control.InvokeRequired 时,这将是有帮助在这种情况下,以及

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天全站免登陆