when thread.sleep() called the visibility of the label is not working properly.
-
this might be a very simple question but i tried to change the visibility of a label with in a timer ticker event. here is the code segment
private void timer1_Tick(object sender, EventArgs e)
{
label1.Visible = false;Thread.Sleep(2000); label1.Visible = true; }
timer interval is set to 1000 and enabled. but the visibility is not working. once it is disappeared it doesn't show again. any idea why this happens.
-
this might be a very simple question but i tried to change the visibility of a label with in a timer ticker event. here is the code segment
private void timer1_Tick(object sender, EventArgs e)
{
label1.Visible = false;Thread.Sleep(2000); label1.Visible = true; }
timer interval is set to 1000 and enabled. but the visibility is not working. once it is disappeared it doesn't show again. any idea why this happens.
Think about it for a moment. You are firing the timer every 1000 ms. When it fires you are telling the thread to sleep for 2000 ms, so if the
Visible = true
line fires it fires while the thread is asleep and so you will not see the effect. Next time the timer fires it makes the label invisible then sleeps again and the whole thing repeats indefinitely. Try settingThread.Sleep(500)
(or any value less than your timer interval).Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus! When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.
-
this might be a very simple question but i tried to change the visibility of a label with in a timer ticker event. here is the code segment
private void timer1_Tick(object sender, EventArgs e)
{
label1.Visible = false;Thread.Sleep(2000); label1.Visible = true; }
timer interval is set to 1000 and enabled. but the visibility is not working. once it is disappeared it doesn't show again. any idea why this happens.
Since you are manipulating GUI components, this must be done on the GUI thread, so your timer has to be a
Windows.Forms.Timer
; however it does not make sense to have a Thread.Sleep() in code that executes on the GUI thread, as that would freeze the GUI (or not work at all). You should implement a state machine, that changes state in the Tick handler without waiting at all. Example: if you want a 5Hz blinking label, set the timer interval to 100, and do:private void timer1_Tick(object sender, EventArgs e) {
label1.Visible = !label1.Visible;
}In the example, the state machine is pretty much hidden as all the state is in the label's visibility itself. Remember: only use
Thread.Sleep()
in threads other than the GUI thread. And most often it is better not to use it at all. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.