计时器在窗体上闪烁消息 [英] timer on form to blink message

查看:137
本文介绍了计时器在窗体上闪烁消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在我的Windows窗体项目中,每当有一个调用长时间运行的方法时,我都会显示一个带有标签控件的小窗体,该控件说,请稍等,然后在长时间运行的调用之后隐藏该窗体.即

Hi,
In my windows form project, every time there is a call to a long running method, I show a small form which has a label control that says, please wait and then after the long running call I hide that form . i.e.

ShowForm();
LongRunningMethod();
HideForm();



问题:
在显示showForm时如何使标签控件每秒闪烁一次?
谢谢



Question:
How can I make the label control to blink every second while the showForm is being shown?
Thanks

推荐答案

我的第一个建议是:不要让任何东西闪烁!大多数用户对此会非常恼火.

如果您不想听这个很好的友好建议,最好在Thread.Sleep中使用单独的线程.至少不要使用System.Windows.Forms.Timer.由于优先级较低(我认为它具有防呆功能),因此它的准确性非常差,以至于您的眨眼有时甚至看起来都不是周期性的.更好地使用其他类型的计时器.

问题是这样的:使用其他计时器和线程,您将必须从非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
My first advice is: don''t make anything blinking! Most users will be highly irritated by this.

If you don''t want to listen to this good friendly advice, better use a separate thread with Thread.Sleep. At least don''t use System.Windows.Forms.Timer. Due to low priority (fool-proof feature, I think), it is so inaccurate that your blinking won''t even look periodic sometimes. Better use other types of timers.

The problem is this: with those other timers and thread, you will have to notify the UI thread form non-UI thread to change color or visibility of some control to blink, etc. But 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[^].

Wait a minute! How about LongRunningMethod? Oh no! You are about to commit a crime (against yourself, I guess). Never ever run anything "long running" in the UI thread. Do it in the separate thread, and also use invocation.

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


您应该在单独的线程中调用LongRunningMethod,并在主线程中使用计时器来使标签闪烁.
请参见线程教程(C#)" [
You should call your LongRunningMethod in a separate thread and use a timer in the main thread to blink the label.
See "Threading Tutorial (C#)"[^].


以以下形式设置计时器:

Set a timer in the form:

            Timer blinkTimer = new Timer();
            blinkTimer.Interval = 1000;
            blinkTimer.Tick += new EventHandler(blinkTimer_Tick);
            blinkTimer.Start();

...

        void blinkTimer_Tick(object sender, EventArgs e)
            {
            myBlinkingLabel.Visible = !myBlinkingLabel.Visible;
            }


这篇关于计时器在窗体上闪烁消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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