CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    May 2012
    Posts
    27

    Different option for Thread.Sleep?

    Well here is my situation. One of my functions has to wait 3 seconds before switching to the next piece of code;

    Code:
    foreach (int restart in md.Restart) {
                       Data.Restart(true);
    I have to wait 3 seconds before it executes this line of code:
    Code:
     Data.Restart(true);
    Thread.Sleep is not an option since it freezes my whole program. Could anyone help me out with a method?

    Any help would be appreciated thanks!

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Different option for Thread.Sleep?

    Spawn a new thread (see the BackgroundWorker class) to carry out the task. Sleeping the background thread will not freeze your whole application.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    May 2012
    Posts
    27

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by BioPhysEngr View Post
    Spawn a new thread (see the BackgroundWorker class) to carry out the task. Sleeping the background thread will not freeze your whole application.
    Sorry, BackGroundWorker is for forms, correct me if I'm wrong. I'm using Console, is there a way to do it that way?

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Different option for Thread.Sleep?

    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    May 2012
    Posts
    27

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by BioPhysEngr View Post
    Thank you for the help so far but wouldn't threading a single thread be really uneffecient instead of using a timer function? That's what I need help with, I don't understand how to use timers. for example.

    I want the timer to start, stop at 3 seconds, execute the one line of code, then reset. could you explain to me how to do that?

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Different option for Thread.Sleep?

    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    May 2012
    Posts
    27

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by BioPhysEngr View Post
    I appreciate the help but this still uses Thread.Sleep. I've already googled many times without any result. The timers I was talking about are the ones you import from System.Timers;

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Different option for Thread.Sleep?

    You have misunderstood the code.

    The first Thread.Sleep() is simply to simulate some amount of work being done while the timer is executing.

    The second Thread.Sleep (in ComputeBoundOp) is called on the timer's thread, not the main thread, because Thread.Sleep() always acts on the thread that called it.

    Also, no; spawning a single thread would not be "really [in]efficient".
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  9. #9
    Join Date
    May 2012
    Posts
    27

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by BigEd781 View Post
    You have misunderstood the code.

    The first Thread.Sleep() is simply to simulate some amount of work being done while the timer is executing.

    The second Thread.Sleep (in ComputeBoundOp) is called on the timer's thread, not the main thread, because Thread.Sleep() always acts on the thread that called it.

    Also, no; spawning a single thread would not be "really [in]efficient".
    Excuse me while I am very new to C#.

    This is what I've done;

    Code:
     Timer t = new Timer(ComputeBoundOp, 5, 0, 2000);
                        //Console.WriteLine("Main thread: Doing other work here...");
                        Thread.Sleep(3000); // Simulating other work (10 seconds)
                        Data.Respawn();
                        t.Dispose(); // Cancel the timer now
    this still freezes all other threads.

    What have I done wrong?

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by wutang001 View Post
    this still freezes all other threads.
    No, it doesn't. It suspends the thread it was called on, no others. You need to remove that Sleep. However, if your program simply exits if you remove the sleep.... then I am perplexed as to why you need another thread in the first place. Hanging the main thread in a console app is fine if it is doing work....

    If you need to wait three seconds, then that three second sleep should be in the timer's callback method.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  11. #11
    Join Date
    May 2012
    Posts
    27

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by BigEd781 View Post
    No, it doesn't. It suspends the thread it was called on, no others. You need to remove that Sleep. However, if your program simply exits if you remove the sleep.... then I am perplexed as to why you need another thread in the first place. Hanging the main thread in a console app is fine if it is doing work....

    If you need to wait three seconds, then that three second sleep should be in the timer's callback method.
    I'm sorry I dont understand what you mean.

    What do I have to do here?

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by wutang001 View Post
    I'm sorry I dont understand what you mean.

    What do I have to do here?
    Please zip up and post the complete sample solution.

  13. #13
    Join Date
    May 2012
    Posts
    27

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by Arjay View Post
    Please zip up and post the complete sample solution.
    This is what I got right now.
    Last edited by wutang001; May 12th, 2012 at 07:22 PM.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Different option for Thread.Sleep?

    I didn't ask for a code snippet, I asked for a sample solution.

    Look, your code snippet isn't sufficient to show the problem you are having so zip up a sample solution that shows the complete problem.

    That way we can load the solution, compile it and may be able to fix what you have into something that works.
    Last edited by Arjay; May 12th, 2012 at 06:13 PM.

  15. #15
    Join Date
    May 2012
    Posts
    27

    Re: Different option for Thread.Sleep?

    Quote Originally Posted by Arjay View Post
    I didn't ask for a code snippet, I asked for a sample solution.

    Look, your code snippet isn't sufficient to show the problem you are having so zip up a sample solution that shows the complete problem.

    That way we can load the solution, compile it and may be able to fix what you have into something that works.
    This is the sample

    http://www.sendspace.com/file/h38g1i

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured