Any difference between kernel32.dll Sleep and Thre

2019-06-21 21:13发布

Is there any difference(performance, implementation. .whatever) between the following:

i)

DllImport("kernel32.dll")]
        public extern static void Sleep(uint msec);

..then call Sleep function

ii)

Thread.Sleep()

2条回答
混吃等死
2楼-- · 2019-06-21 21:44

There's a big difference, actually.

This blog post explains why managed threads should never do unmanaged blocking, if possible. The official MSDN documentation has the same guideline without all the underlying details.

P.S. Thread.Sleep is a sign of a poorly-designed program.

查看更多
Evening l夕情丶
3楼-- · 2019-06-21 21:56

I wouldn't think so. They both work on the current thread only.

Take care though when using the Sleep function in the main thread as programs that depend on synchronized timing usually cause problems when external configurations change.

Using Sleep in support or listening threads is usually not a problem though.

查看更多
登录 后发表回答