开发者

How can I update a winform asynchronously?

I've got a winform, and a bluetooth connection with a lego n开发者_运维百科xt 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.


you need to invoke your label updates back to the main thread

see Dispatcher.Invoke

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜