开发者

simpler C# code to introduce 1 mSec delay?

开发者 https://www.devze.com 2023-01-10 15:29 出处:网络
I am not using Thread so can\'t use t开发者_JAVA技巧hread.sleep() method.. Its part of my program where I need to introduce some delay .. Not precisely 1mSec but almost that ..

I am not using Thread so can't use t开发者_JAVA技巧hread.sleep() method.. Its part of my program where I need to introduce some delay .. Not precisely 1mSec but almost that ..

which is the standard method that is known to be so??


You are always using a thread. Every application has at least one thread, so Thread.Sleep will work fine.


I am not using Thread so can't use thread.sleep() method

Not sure you you say that -- you can use Thread.Sleep anywhere you like.


Just so you know, a 1msec sleep isn't guaranteed be exactly 1msec, in fact it's very improbable due to it being such a small time. The thread.sleep(x) states x as a minimum sleep time, if you wan't a much more exact sleep you might want to look into win32 multimedia timers: http://msdn.microsoft.com/en-us/library/dd742877.aspx


Don't sleep 1msec. It will not be accurate at all. Read for instance this article or this

Thread.sleep will always suspend the current thread. Keep in mind that it's not a good idea to use Sleep on a GUI thread (if your app is a winform app).


If you are just trying to provide an opportunity for thread switching, then use Thread.Sleep(0). (This is equivalent to Thread.Yield() in Java.)

Edit: actually seems like they've added Thread.Yield() in .NET 4.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号