为什么我要使用无限超时的 Sleep() ?

发布于 2024-08-14 12:39:10 字数 213 浏览 4 评论 0原文

根据 MSDN, Sleep() 可以是提供了 INFINITE 值并且“表明暂停不应超时”。

为什么我要在程序中调用带有 INFINITE 超时的 Sleep() ?

According to MSDN, Sleep() can be provided INFINITE value and that "indicates that the suspension should not time out".

Why would I want to call Sleep() with INFINITE timeout in my program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

还不是爱你 2024-08-21 12:39:10

我使用过 Sleep(INFINITE),它非常有意义。我用它来保持线程的活力。我已经注册了 WMI 通知事件(ExecNotificationQueryAsync,它无限地接收事件通知),那么您需要保持应用程序处于活动状态。不知道这对你是否有意义。

I have used Sleep(INFINITE) and it makes perfect sense. I've used it to keep the thread alive. I have registered for WMI notification event (ExecNotificationQueryAsync, which receives event notification infinitely) then you need to keep the application alive. dont' know if this make sense to you.

人间不值得 2024-08-21 12:39:10

没有超时的睡眠不需要计时器。这减少了您预期可变长度等待但绝对确定线程将恢复的开销。

A sleep with no timeout does not need a timer. This reduces the overhead where you anticipate a variable-length wait but are absolutely sure that the thread will be resumed.

风和你 2024-08-21 12:39:10

据我所知,Sleep,自从他们引入了SleepEx以来,它只是SleepEx的一个薄而方便的包装器,当他们将其重写为包装器时,他们决定只是将超时参数传递给SleepEx,而不对其进行任何处理。显然,以这种方式,具有 INFINITE 作为超时的函数的行为也会传播到 Sleep (因此必须记录),尽管如果没有 SleepEx 的 bAlertable 参数,它是完全无用的( Sleep(timeout) 等于 SleepEx(超时,FALSE),因此您将有无限的不可警报的等待)。

那么,在 Windows CE 上,他们可能决定改变这种行为,因为这实际上很愚蠢,所以我认为 CE 上的 Sleep(INFINITE) 会自动转换为 SuspendThread;然而,在 Windows 上,出于兼容性原因,它们可能被迫保持“简单”行为。

As far as I know, Sleep, since they introduced SleepEx, it's just a thin, convenient wrapper around SleepEx, and when they rewrote it as a wrapper, they decided just to pass the timeout parameter to SleepEx, without processing it in any way. Obviously in this way the behavior of the function with INFINITE as timeout is propagated also to Sleep (and so must be documented), although, without the bAlertable parameter of the SleepEx, it's completely useless (a Sleep(timeout) is equal to SleepEx(timeout, FALSE), so you'll have an infinite nonalertable wait).

On Windows CE, then, they may have decided to change this behavior because it was actually silly, so I think that a Sleep(INFINITE) on CE is translated automatically to a SuspendThread; however, on Windows they are probably forced to keep the "simple" behavior for compatibility reasons.

水中月 2024-08-21 12:39:10

除了所说的(基本上是等待中断发生)之外,您很可能会遇到无限超时而不会发疯。例如,我有一个应用程序(一个工作程序)需要一次执行 3 件不同的事情。

我选择让每个工作在新线程中运行,并在 Main() 线程中具有无限超时(因为程序不应该退出,除非抛出异常,在这种情况下整个应用程序将重新启动),对于方便性和可读性(我可以注释掉这 3 个作品中的任何一个,而不影响全局行为,或者根据需要轻松地将它们拆分给不同的工作人员)。

与 2 个新线程 + 主线程执行第三项工作相比,这可能会增加非常小的开销,但考虑到当今计算机的性能和内存,它可以忽略不计。

In addition to what was said (basically waiting for an interrupt to happen) You might very well have an infinite timeout without being insane. For example, I've an application (a worker) that needs to do 3 different things at a time.

I chose to make each of those work run in new threads and have an infinite timeout in the Main() thread (as the program is not supposed to exit, except if an Exception is thrown in which case the whole app is restarted), for convenience and readability (I can comment out any of the 3 works without affecting the global behavior or easily split them to different workers if needed).

This probably adds a very small overhead compared to have 2 new thread + the main thread doing the 3rd work, but it's negligible considering today computers performances and memory.

累赘 2024-08-21 12:39:10

一个心智正常的人没有理由去睡觉(无限)。它没有实际意义。

这是为了 WaitForSingleObject(..., timeout) 和 SleepEx(timeout) 的通用性和对称性,其中 INFINITE 确实有意义。

提醒一下,SleepEx 将尝试消耗线程 APC 队列中的内容。

There's no reasons one in his sane mind would ever Sleep(INFINITE). It has no practical meaning.

It is for generality and symmetry to WaitForSingleObject(..., timeout) and SleepEx(timeout), where INFINITE does make sense.

Reminding, that SleepEx will try to consume things out of your thread's APC queue.

逆蝶 2024-08-21 12:39:10

好吧,当我们需要等到 ^C 但我们不想要 while(1); 时

Well, When we need to wait until ^C but we do not want while(1);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文