1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[C#]After X Seconds...

Discussion in 'C, C++, C#' started by hwolfpack6, Aug 22, 2009.

  1. hwolfpack6

    hwolfpack6 BANNED BANNED

    Joined:
    Dec 11, 2008
    Messages:
    646
    Likes Received:
    175
    I have a question, and I feel it's very basic:

    After X Seconds, label1.visible = true;

    Sounds easy, right? How can I measure time? I'm really new to C#.
     
  2. letusgo

    letusgo Junior Member

    Joined:
    Nov 15, 2008
    Messages:
    199
    Likes Received:
    125
    using System.Threading;
    // ...
    Thread.Sleep(50);
     
  3. hwolfpack6

    hwolfpack6 BANNED BANNED

    Joined:
    Dec 11, 2008
    Messages:
    646
    Likes Received:
    175
    Does label1.visible = True go in the // ... space or do I make it sleep first then do the visible stuff?
     
  4. soulchief

    soulchief Junior Member

    Joined:
    Oct 17, 2007
    Messages:
    117
    Likes Received:
    55
    Location:
    Canada
    put it after Thread.sleep(50)

    // = comment..

    and 50 = .05 seconds, so may want to increase that.
     
  5. hwolfpack6

    hwolfpack6 BANNED BANNED

    Joined:
    Dec 11, 2008
    Messages:
    646
    Likes Received:
    175
    Code:
            private void Form2_Load(object sender, EventArgs e)
            {
                using (System.Thread) ;
                Thread.Sleep(50);
                label1.visible = False;
                label2.visible = True;
    
            }
    That look correct? I'm getting Possible Mistaken Empty Statement :(
     
  6. soulchief

    soulchief Junior Member

    Joined:
    Oct 17, 2007
    Messages:
    117
    Likes Received:
    55
    Location:
    Canada
    using System.Threading; should be before the private void Form2_Load
     
  7. hwolfpack6

    hwolfpack6 BANNED BANNED

    Joined:
    Dec 11, 2008
    Messages:
    646
    Likes Received:
    175
    Cool, we're almost there. It's loaded now, but when I click on the install button to move from Form1 to Form2, I think that's when it's getting the 5000 millisecond sleep.
     
  8. hwolfpack6

    hwolfpack6 BANNED BANNED

    Joined:
    Dec 11, 2008
    Messages:
    646
    Likes Received:
    175
    Any solution to this? It still sleeps for 5 seconds after clicking install, which essentially just makes it so that form 1 doesn't disappear and form 2 doesn't appear for 5 seconds.
     
  9. doseph

    doseph Registered Member

    Joined:
    Feb 23, 2008
    Messages:
    86
    Likes Received:
    24
    Home Page:
    Use a timer control, not thread.sleep. That's never how thread.sleep should be used, as it will delay anything from happening.

    Add a timer control to your form, set the interval to 5000 or whatever you want, enabled = false, and then on form load, set enabled = true.

    Put the code you want to run after 5 seconds in the timer_tick event, and unless you want it to keep running every 5 seconds, set the timer.enabled = false again.