Jump to content
  • Advertisement
Sign in to follow this  
Mashew

Thread.SpinWait() [C#] (SOLVED)

This topic is 3721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm wanting my program to wait a certain amount of time but, I have heard Thread.SpinWait is bad to use for some reason. Why is it bad to use and what is something else I could use? EDIT: I solved the problem! Thanks for the help! [Edited by - Mashew on March 15, 2008 8:40:30 AM]

Share this post


Link to post
Share on other sites
Advertisement
Why don't you simply use System.Threading.Thread.Sleep() ? You can specify the number of milliseconds to wait and (if I understood the documentation right) you're not losing processor time in a tight loop (which's execution speed depends on your processor speed).

Unless you have very special needs, Thread.Sleep would be the way to go

Share this post


Link to post
Share on other sites
If you want to know something about a certain method, look it up, and see what the documentation has to say.

Thread.SpinWait

Quote:
The SpinWait method is useful for implementing locks. Classes in the .NET Framework, such as Monitor and ReaderWriterLock, use this method internally. SpinWait essentially puts the processor into a very tight loop, with the loop count specified by the iterations parameter. The duration of the wait therefore depends on the speed of the processor.

Contrast this with the Sleep method. A thread that calls Sleep yields the rest of its current slice of processor time, even if the specified interval is zero. Specifying a non-zero interval for Sleep removes the thread from consideration by the thread scheduler until the time interval has elapsed.

SpinWait is not generally useful for ordinary applications. In most cases, you should use the synchronization classes provided by the .NET Framework; for example, call Monitor..::.Enter or a statement that wraps Monitor..::.Enter (lock in C# or SyncLock in Visual Basic).

Share this post


Link to post
Share on other sites
What kind of "input" are you monitoring?

You could always do :

while(!InputReceived())
Thread.Sleep(0);


By the way, if you're talking about a windows form application, using Thread.Sleep will stop your forms from responding, so you'd better make a loop which calls System.Windows.Forms.Application.DoEvents();

EDIT : Removing your posts when a solution has been found isn't a good idea : they could've helped someone with the same problem to solve it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!