Sleeping without pausing the main thread

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Sleeping without pausing the main thread

 
0
  #1
Jan 2nd, 2009
Well im trying to "sleep" or make the program twiddle its thumbs for 15 seconds before continueing but without pausing the main thread.

So i thought maybe if i create a new thread and Sleep in that one that would help but no.

So then i thought the timer object but still no.

Now i dont know if the above "no"'s are because im completely stupid and cant use them or they didnt actually work.....

Heres the new thread method i stole from microsoft.

  1. class Sleep
  2. {
  3. static TimeSpan waitTime = new TimeSpan(0, 0, 15);
  4.  
  5. public static void sSleep()
  6. {
  7. Thread newThread = new Thread(new ThreadStart(Sleeping));
  8. newThread.Start();
  9.  
  10. if (newThread.Join(waitTime + waitTime))
  11. {
  12. //
  13. }
  14. else
  15. {
  16. //
  17. }
  18. }
  19. static void Sleeping()
  20. {
  21. Thread.Sleep(waitTime);
  22. }
  23. }

That didnt work for me (it paused the main thread)

If someone could show me how to use the timer correctly that would be much appriciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Sleeping without pausing the main thread

 
0
  #2
Jan 2nd, 2009
You could just use a timer.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: Sleeping without pausing the main thread

 
0
  #3
Jan 2nd, 2009
Originally Posted by LizR View Post
You could just use a timer.
as i said if someone could show me how to use the timer correctly that would be very very helpful
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 49
Reputation: iDeveloper is an unknown quantity at this point 
Solved Threads: 7
iDeveloper iDeveloper is offline Offline
Light Poster

Re: Sleeping without pausing the main thread

 
0
  #4
Jan 3rd, 2009
I don't understand your problem. You're trying to make your program 'sleep' without pausing the main thread? That's not really a sleep because sleep would be a blocking call. If what you need is a timer, then use the Timer class. Here is how it looks in short:

  1.  
  2. Timer mTimer = new Timer(TIMEOUT_VALUE);
  3. mTimer.Elapsed += new ElapsedEventHandler(TIMEOUT_METHOD);
  4. mTimer.Start();

Here is the signature for TIMEOUT_METHOD

  1.  
  2. public void MyMethod(object source, ElapsedEventArgs e)
  3. { }

This method will execute when your timer ends. The timer will then restart automatically when the call to this method completes.
Last edited by iDeveloper; Jan 3rd, 2009 at 12:04 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Sleeping without pausing the main thread

 
0
  #5
Jan 3rd, 2009
Timer is also a component on your toolbar - with other examples on how to use it in your helpfile.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,913
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Sleeping without pausing the main thread

 
0
  #6
Jan 3rd, 2009
Perhaps if you let the main thread spawn a new thread. You could pause the new thread and let the main thread continue?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC