Threading.Timer与WinForm [英] Threading.Timer with WinForm

查看:68
本文介绍了Threading.Timer与WinForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每隔1/2秒通过Threading.Timer轮询一个数据库,并在Windows窗体列表视图控件中显示结果。查询结果返回约150行,我首先将ADD添加到listview。填充列表视图后,我不再添加项目,我遍历列表视图的每一行并访问TEXT属性以更改每行的内容。我这样做所以我不必清除每个查询的listview,导致listview绘制并丢失它当前的滚动位置...



winForm是一个一点反应迟钝。我想如果我将控件的更新移动到主线程,表单会说响应,这会改善性能。



有人可以澄清一下是什么在这里,我需要做些什么来使表单更具响应性并提高使用新结果更新listview的效率,但不重新绘制listview并丢失它当前的滚动位置。



提前致谢,

-Dee

I'm polling a database every 1/2 second via Threading.Timer and displaying the results in a Windows Form listview control. The query results return about 150 rows that I initial 'ADD' to listview. Once the listview is populated I no longer 'ADD' items I iterate through each row of the listview and access the TEXT property to change the contents of each row. I do this so I won't have to clear the listview on each query causing the listview to paint and lose it's current scroll position...

The winForm is a little unresponsive. I thought if I moved the updating of the control off of the main thread that the form would say responsive and this would improve the performance.

Can someone please clarify what's going on here and what do I need to do to make the form more responsive and improve the efficiency of updating the listview with new result but without repainting the listview and losing it's current scroll position.

Thanks in advance,
-Dee

推荐答案

我的解决方案有什么问题带定时器的轮询数据库 [ ^ ]?



如果你单独进行轮询线程,UI更新将通过UI线程调用。即使你更新主线程的控件(我把它读作UI线程),由于调用,实际更新将在UI线程中发生。您根本无法从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
What's wrong about my solution Polling Database with Timer[^]?

If you run polling in a separate thread, UI update will go via UI thread invocation. Even it you "updating of the control off of the main thread" (I read it as "UI thread"), the actual update will happen in UI thread due to invocation. You simply 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


尝试将double buffered属性设置为true。



你也可以使用:








Try setting the double buffered property in true.

Also you could use:




this.SuspendLayout();
this.listview.SuspendLayout();







//update control  
//put listview manipulation code hrer to not cause refresh form










this.listview.ResumeLayout(false);
this.ResumeLayout(false);


这篇关于Threading.Timer与WinForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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