System.Timer能否破坏多线程UI [英] Can System.Timer disrupt multithread UI

查看:123
本文介绍了System.Timer能否破坏多线程UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个winForm,可以查询数据库并呈现dataGridView控件.查询和控件渲染是在单独的线程上完成的,并且通过System.Timer调用的查询...我的问题是:如果在下一个timerEvent触发时查询未完成,将导致UI(main)上的性能问题即使我在一个单独的线程上执行查询和渲染,该线程也是如此?

预先感谢,
-da

Hi,

I have a winForm that queries a database and renders a dataGridView control. the queries and control rendering are done on a separate thread and the queries called via System.Timer...my question is: if the query is not complete by the time the next timerEvent fires will that cause performance issue on the UI(main) thread even though I''m preforming the query and rendering on a separate thread?

thanks in advance,
-da

推荐答案

是的,可以.您不能从计时器的事件处理程序中调用任何UI方法或属性,因为不能保证在UI线程中调用它.

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

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

—SA
Yes it can, in a way. You cannot call any UI methods or properties from the event handler of the timer as you are not guaranteed to call it in the UI thread.

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[^].

You can easily use proper invocation of delegated to the UI thread; and it should not be a problem. However…

Make sure you look at this problem from a different angle. Are you sure you need to use a timer at all? There are many cases when you would be better off using the separate thread instead of the timer. It is more straightforward and easier to implement and debug. Besides, there can be serious logical problem with thread. Did you ever address the situation where a new timer event is raised when you event handler is still being executed? How to deal with that?

In one of my past solutions, I offered a pretty nice way of combining a timer and a separate thread for polling of a database. Please see Polling Database with Timer[^].

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''s basically two of the same operation running side by side if that happens many times I would imagine performance would be affected. If I were you I would disable the timer on each call then re-enable after query is complete.


这篇关于System.Timer能否破坏多线程UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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