使用计时器,我的应用程序会减慢处理速度,如何解决此问题? [英] Using timer my application will slow process, How to resolve this?

查看:96
本文介绍了使用计时器,我的应用程序会减慢处理速度,如何解决此问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai guys,

在我的项目中,我正在使用3个不同的计时器控件来逐个移动3个不同的listviews项目...因此我的应用程序显示过程非常缓慢.我无法单击其他按钮,停止计时器后,只有其他控件才能正常工作.


我该如何解决这个问题?有什么解决办法吗?

问候
vasanth

hai guys,

In my project i am using 3 different timer control for move 3 different listviews items one by one... so my application shows very slow process. I can''t able to click other button, After stopped the timer only other controls are work without time gap.


How can i resolve this problem? Is there any solutions?

Regards
vasanth

推荐答案

计时器通常不好.一个计时器System.Windows.Forms.Timer是最简单的计时器,但提供的精度却令人无法接受(在少数情况下还可以,但不像您的计时器那样好),其他两个计时器则更好,但它们困扰很多.您是否曾经尝试解决当计时器事件处理程序仍在执行时调用新计时器事件的情况?

线程是更直接的方法,因为逻辑是线性的.您需要使用Thread.Sleep创建具有延迟的循环的单独线程.

首先,我建议使用线程包装器解决将参数传递给它的问题.请查看我过去的解决方案:
如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].

现在,您需要在每个周期中更新您的UI.问题是:您不能从非UI线程调用与UI相关的任何东西.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).



顺便说一句,无论如何,两个普通"计时器(System.Windows.Forms.Timer除外)都需要上面描述的UI线程调用,因此您不能通过不执行调用来赢得计时器.

[END EDIT]

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

—SA
Timers are generally bad. One timer, System.Windows.Forms.Timer is the easiest but provide unacceptable accuracy (there are rare cases when that''s all right, but not like yours), other two are better, but there is a lot of bothering with them. Did you ever try to account for the situation when a new timer event is invoked when your timer event handler is still executing?

Threads is much more straightforward way, because the logic is linear. You need to create a separate thread with a loop with some delay using Thread.Sleep.

First of all, I recommend using a thread wrapper to solve the problem of passing parameters to it. Please see my past solutions:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

Now, you need to update your UI in each cycle. On problem is: 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).



By the way, two "normal" timers (other than System.Windows.Forms.Timer) will need UI thread invocation described above anyway, so you cannot win with a timer by not doing the invocation.

[END EDIT]

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



我认为如果您使用多线程编程,它将可以正常工作.

祝你好运
Hi
I think if u use multi thread programming , it''ll work.

Good Luck


这篇关于使用计时器,我的应用程序会减慢处理速度,如何解决此问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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