使用线程将项目添加到列表视图 [英] Add Items to a List View with Thread

查看:114
本文介绍了使用线程将项目添加到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量搜索和搜索"后,我迷失了如何从线程更新列表视图的确切信息.

对象:扫描计算机是否存在"* .jpg",并在运行时将其显示在列表视图中

我的搜索正确,但是到目前为止,我对如何启动正确的线程并在运行时更新列表感到困惑.

After doing much searching and "googling", I am lost on exactly how to update a list view from a thread.

The Object: Scan computer for "*.jpg" and display them in a list view at runtime

I have the correct search but as of right now I am confused on how to start the correct thread and update the list at runtime

推荐答案


如果使用的是WPF,则可以使用Application.Current.Dispatcher.Invoke将执行封送回主线程,并使用Action参数指定要在主线程中调用的方法.

Uros
Hi
if you are using WPF you can marshal execution back to main thread with Application.Current.Dispatcher.Invoke and use the Action argument to specify which method you want to invoke in the main thread.

Uros


您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于使用线程将项目添加到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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