943,793 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2510
  • C# RSS
Jan 2nd, 2009
0

Sleeping without pausing the main thread

Expand Post »
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.

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jan 2nd, 2009
0

Re: Sleeping without pausing the main thread

You could just use a timer.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 2nd, 2009
0

Re: Sleeping without pausing the main thread

Click to Expand / Collapse  Quote originally posted by LizR ...
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
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Jan 3rd, 2009
0

Re: Sleeping without pausing the main thread

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:

C# Syntax (Toggle Plain Text)
  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

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 31
Solved Threads: 7
Light Poster
iDeveloper is offline Offline
49 posts
since Jul 2008
Jan 3rd, 2009
0

Re: Sleeping without pausing the main thread

Timer is also a component on your toolbar - with other examples on how to use it in your helpfile.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 3rd, 2009
0

Re: Sleeping without pausing the main thread

Perhaps if you let the main thread spawn a new thread. You could pause the new thread and let the main thread continue?
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,738 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Help with Logic
Next Thread in C# Forum Timeline: How to GetPixel from Bitmap?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC