Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. when thread.sleep() called the visibility of the label is not working properly.

when thread.sleep() called the visibility of the label is not working properly.

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    prasadbuddhika
    wrote on last edited by
    #1

    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.

    H L 2 Replies Last reply
    0
    • P prasadbuddhika

      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.

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      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 setting Thread.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.

      1 Reply Last reply
      0
      • P prasadbuddhika

        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.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups