Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts

    C#: Thread.Sleep(3000) executes too soon?

    Hey, I'm back!

    I have a form with a button whose text changes right before Thread.Sleep(3000). The issue that I am having is, the sleep begins before the form button has time to update its self.

    Code:
    button.text = "words";
    ...
    button.text = "new words";
    Thread.Sleep(3000);
    button.text = "words";
    It's actually part of a game. The button resets itself after the sleep is done, but sense the initial change doesn't occur until right before it reverts back to the way it was, it doesn't even flicker!

    I hope it is just something stupid, but I get the feeling it is not...

    Thanks for any and all help!
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    That is because more than likely you are sleeping the main application thread which is responsible for repainting the form. Instead of sleeping the main thread, you should use a timer instead or create a worker thread and run your code in that.
    OracleGuy

  • #3
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Is it possible to use a timer to pause the program?

    The Thread.Sleep(3000) is in the form its self, so it would make sense that it is pausing the main thread. I don't know what kind of repercussions exist for making a new thread. Threads are a completely new concept to me.

    If it is possible to use a timer, could someone please explain how I can use it to make the program wait three seconds before it continues? Or even another way.

    I've looked up timers and what I have found hasn't helped one bit! I was thinking maybe a while loop, but I really just don't understand.

    Thanks again for your help!
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    The way timers work is that you set an interval and start them and they call a function of your choosing at that interval. If you only want it to happen once, you turn it off in the function when it gets called.

    The exact approach to using it depends on your program, the same applies if you wanted to use a separate thread.
    OracleGuy

  • #5
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Yeah, thanks. I did figure out how to make a timer. I don't understand why I had such a hard time with it. It wasn't very hard....

    Hey, isn't there a topic solved button or something? I thought there was, but I can't find it...
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Edit your first post, there should be a drop down next to the thread title.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •