按钮背景色闪烁不均匀 [英] Flashing of button backcolor is not consistent

查看:87
本文介绍了按钮背景色闪烁不均匀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

谁能告诉我为什么即使我尝试使用backgroundwoker事件执行按钮背景色时也没有更改.我正在从串行端口中获取数据,并根据收到的数据在UI中执行某些操作,UI显示响应缓慢,甚至在该操作过程中有时会停止闪烁.任何人都可以告诉我如何完成任务.我有一个运行时间为500ms的计时器执行此背景色更改操作.我已经以Windows形式开发了此UI.

在此先谢谢您.

Hi all,

Can anybody tell me why the changing of button backcolour is not even when i am trying to execute it using backgroundwoker event. I am fetching data from serial port & based on the data that i receive i am performing certain action in the UI, UI shows a slow response & even sometimes flashing stops during that operation. So can anybody tell me how to achieve the task. I have a timer running @ 500ms performs this backcolor changing operation. I hav developed this UI in windows form.

Thanks in advance.

推荐答案

您解释问题的方式没有任何帮助.没有代码示例,没有确定的内容.我们甚至不知道您使用哪种计时器类型;他们都是非常不同的.还不清楚为什么同时使用一个单独的线程(通常不是一个坏主意)和一个计时器.

但是,一些提示给您.如果使用的计时器System.Windows.Forms.Timer(可能是最简单的计时器),则不能期望有任何合理的精度.如果期望使用此计时器进行一些定期处理,您可能会感到沮丧:运动可能不是周期性的,甚至可能是很长的冻结时间,这也取决于您的应用程序时间.您最好使用单独的线程或任何其他计时器类型.

但是,您需要知道,使用其他计时器类型和非UI线程,您将无法直接更新UI.您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

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

最后,我将谈谈您的串行端口通信.首先,还建议在单独的线程而不是UI线程上进行通信.另外,这完全取决于RS-232电缆另一端的情况.通过此电缆连接的设备也可能会延迟您的处理;我不知道你的情况是什么.您没有在问题中解释它.

—SA
The way you explain your concerns is not informative. No code sample, no anything certain. We don''t even know what timer type do you use; they are all very different. And it''s not clear why using a separate thread (not a bad idea, generally) and a timer at the same time.

However, some hints for you. If you are using the timer System.Windows.Forms.Timer, which is probably the simplest in use, you cannot expect any reasonable accuracy. If expected some periodic processing driven by this timer, you could be frustrated: the motion could be far from periodic, and even with the apparently long periods of freezing, which depends on your application timing. You should better use a separate thread or any other timer type.

However, you need to know that with other timer types and a non-UI thread, you cannot update your UI directly. 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[^].

And my final note will be about your serial port communication. First, it is also recommended to do the communication on a separate thread, not a UI thread. Also, it all highly depends on what happens on the other end of your RS-232 cable. The device connected by this cable could also delay your processing; I have no idea what is that in your case; you did not explain it in your question.

—SA


这篇关于按钮背景色闪烁不均匀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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