如何使用backgroundworker在不同的表单上更新listview [英] How to update listview on a different form using backgroundworker
本文介绍了如何使用backgroundworker在不同的表单上更新listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我的后台工作者从txt文件数据库中读取数据,并将数据放在主窗体上的列表视图中。列表视图中没有出现任何内容,我证明数据已经发布但由于线程错误等原因未写入,但我不知道如何使其工作。
-Rixterz
解决方案
除了创建它们的线程之外,您无法访问UI元素:UI线程。当你尝试时,你得到一个跨线程异常 - 如你所见。
幸运的是它很容易修复:只需隐藏 [ ^ ]来自后台工作程序的控件,它会将执行移动到UI线程上。
Hello,
My backgroundworker reads from a txt file database and should put the data in a listview on the main form. Nothing came up in the listview, and I proved that data was going to it but was not written due to threading errors and such, but I don't know how to make it work.
-Rixterz
解决方案
You can't access UI elements except from the thread on which they were created: the UI thread. When you try, you get a cross thread exception - as you have seen.
Fortunately it's simple to fix: just Invoke[^] the control from the background worker, and it will move the execution onto the UI thread for you.
这篇关于如何使用backgroundworker在不同的表单上更新listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文