| | |
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 barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum event excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile gis globalization gtk httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml






