how to suspend a thread for 1 ms?

后端 未结 2 593
轻奢々
轻奢々 2021-01-24 03:07

I need prety simple thing

while (true) {
    DoJob();
    // wait 1 ms
}

Should I just use Thread.Sleep(1)? I\'m not sure about us

2条回答
  •  太阳男子
    2021-01-24 03:43

    There may be an alternative way to do this. Windows has a high resolution timer. You can read about it here: "How to: Use the high resolution timer"

    I don't think that this works like a regular timer -- it doesn't fire off events, you just use it to measure how much time has passed with high precision. However, you could loop on it, and execute code when a Millisecond passes (you will be consuming CPU the whole time). Also, I agree with Henk's comments that Windows is not a realtime O/S -- you never know when the O/S will suspend your thread.

提交回复
热议问题