| | |
Sleeping without pausing the main thread
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 99
Reputation:
Solved Threads: 1
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.
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.
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)
class Sleep { static TimeSpan waitTime = new TimeSpan(0, 0, 15); public static void sSleep() { Thread newThread = new Thread(new ThreadStart(Sleeping)); newThread.Start(); if (newThread.Join(waitTime + waitTime)) { // } else { // } } static void Sleeping() { Thread.Sleep(waitTime); } }
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.
•
•
Join Date: Jul 2008
Posts: 49
Reputation:
Solved Threads: 7
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:
Here is the signature for TIMEOUT_METHOD
This method will execute when your timer ends. The timer will then restart automatically when the call to this method completes.
C# Syntax (Toggle Plain Text)
Timer mTimer = new Timer(TIMEOUT_VALUE); mTimer.Elapsed += new ElapsedEventHandler(TIMEOUT_METHOD); mTimer.Start();
Here is the signature for TIMEOUT_METHOD
C# Syntax (Toggle Plain Text)
public void MyMethod(object source, ElapsedEventArgs e) { }
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.
![]() |
Other Threads in the C# Forum
- Previous Thread: Help with Logic
- Next Thread: How to GetPixel from Bitmap?
| Thread Tools | Search this Thread |
.net access algorithm array asp.net barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net httpwebrequest image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql mysql.data.client operator path photoshop php picturebox pixelinversion post programming radians regex remoting resourcefile richtextbox server sleep socket sql statistics stream string study system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml






