C# 中的固件_nop()
我正在将部分固件移植到用 C# 编写的 GUI 中。我发现这个
nop()
函数实际上什么都不做,只是将执行延迟了一定数量的时钟周期。
假设我使用 nop() ,它将延迟固件中的执行两毫秒。如何在 C# 中延迟事件?
提前致谢,
I am porting part of firmware to a gui written in c#. I found out that
nop()
is a function that actually does nothing and delays the execution by some number of clock cycles.
Lets say if I use nop() it will delay the execution in firmware by two milliseconds. How do I delay things in c#?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
System.Threading.Thread.Sleep(milliseconds);
请注意,阻塞 GUI 主线程超过几毫秒通常是一个坏主意。最好使用一个在几毫秒后唤醒的计时器,以便同时进行事件处理。另外,如果您想在执行某些 I/O 之前暂停,请考虑使用异步 I/O。
另请注意,Windows 或 .NET 都不提供任何实时保证。对 Thread.Sleep(2) 的调用将暂停至少 2 毫秒,但该线程可能不会在超过该时间的许多毫秒内恢复执行。
System.Threading.Thread.Sleep(milliseconds);
Note that blocking your GUI's main thread for more than a few milliseconds is generally a bad idea. It may be better to use a timer that wakes up after a few milliseconds, to allow event-handling to happen in the meantime. Also, if you want to pause before doing some I/O, consider using asynchronous I/O instead.
Also note that neither Windows or .NET provide any real-time guarantees. A call to
Thread.Sleep(2)
will pause for at least 2 milliseconds, but that thread may not resume execution for many milliseconds beyond that.将使当前执行休眠 1 秒。
Will sleep the current execution for 1 second.
Thread.Sleep。
例如:
Thread.Sleep(2);
请注意,Windows 的最小分辨率约为 15 毫秒,因此您可能不会达到 2 毫秒。有关详细信息,请参阅Thread.Sleep(1) 的影响是什么C#?
Thread.Sleep.
For example:
Thread.Sleep(2);
Be aware that windows has a minimum resolution of around 15ms however, so you might not get exatly 2ms. For more info, see What is the impact of Thread.Sleep(1) in C#?
我要反其道而行之,如果你需要严格的计时,Thread.Sleep 不是一个好主意。
我建议将手动/自动 ResetEvents 与
System.Timers
计时器代替。Gonna go against the grain and say, if you need strict timing, Thread.Sleep isn't a good idea.
I recommend using Manual/Auto ResetEvents with a
System.Timers
timer instead.