Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
4.17/5 (3 votes)
See more:
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.
C#
ShowForm();
LongRunningMethod();
HideForm();


Question:
How can I make the label control to blink every second while the showForm is being shown?
Thanks
Posted
Updated 5-Feb-12 22:06pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Feb-12 4:15am    
Just a note: this code would be serious abuse of... I don't know, pretty much everything. Please see my solution.
--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
 
Share this answer
 
Comments
CPallini 6-Feb-12 4:20am    
"My first advice is: don't make anything blinking!"
Thank you for the advice: I've just fixed my car's blinkers... :-)
Sergey Alexandrovich Kryukov 6-Feb-12 4:22am    
Ha-ha! Good point, and thank you. "Never say never"...
--SA
arkiboys 6-Feb-12 4:32am    
Thanks
Sergey Alexandrovich Kryukov 6-Feb-12 4:39am    
You are welcome.
If you agree that it makes sense, please accept the answer formally (green button); you can accept more than one -- thanks.
--SA
Espen Harlinn 6-Feb-12 6:31am    
Good reply :)
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#)"[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Feb-12 4:21am    
Good point, but I'm not fully agree with the timer. Also, it depends on the type of timer. (I voted 4).

For explanation, please see my comment to the answer by Griff and my own answer.
--SA
thatraja 6-Feb-12 13:29pm    
5!
Set a timer in the form:

C#
            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;
            }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Feb-12 4:19am    
Griff, did you try it? It is not unusual when blinking made using System.Windows.Forms.Timer does not look periodic, due to low priority and hence accuracy of this timer type. This timer is useful sometimes, but a human eye can easily see this flaw of this timer in wanna-be-periodic action. I saw it a number of times.
(I did not vote.)

Please see my solution -- more complex but robust.
--SA
arkiboys 6-Feb-12 4:30am    
Following what you mentioned.
But the label does not blink.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900