简体   繁体   English

C++ 中的“交互式”休眠

[英]“Interactive” Sleep in C++

I started to learn c++ a while ago.前段时间开始学习c++。 I done a lot of console program with Visual Studio 2010 and now I want to make win32 programs.我用 Visual Studio 2010 做了很多控制台程序,现在我想制作 win32 程序。 I read a lot of tutorial and I understand the basics now.我阅读了很多教程,现在我了解了基础知识。

I made a button which call another function.我做了一个按钮,调用另一个 function。 The function is doing great everything working. function 一切正常。 But there are Sleep(x) in this function and the GUI isn't responding while the Sleep doesn't end.但是这个 function 中有 Sleep(x) 并且在睡眠没有结束时 GUI 没有响应。 The function is done in 60 sec because there is a lot of Sleep in it but it is do everything what is should just do GUI isn't responding this time. function 在 60 秒内完成,因为其中有很多睡眠,但它完成了所有应该做的事情,这次 GUI 没有响应。

I think I know the source of this problem.我想我知道这个问题的根源。 In my opinion the problem is Sleep totally pause the application (Windows say: not responding).在我看来,问题是睡眠完全暂停了应用程序(Windows 说:没有响应)。

How can I make a Sleep/pause which doesn't freeze the GUI?如何进行不冻结 GUI 的睡眠/暂停? Is there a function or I must do it in a totally different way?是否有 function 或者我必须以完全不同的方式进行操作?

Thank you in advance!先感谢您!

In my opinion the problem is Sleep totally pause the application在我看来,问题是睡眠完全暂停应用程序

Yes.是的。

How can I make a Sleep/pause which doesn't freeze the GUI?如何进行不冻结 GUI 的睡眠/暂停?

Why do you need to sleep in the first place?为什么你首先需要睡觉? If the function is misbehaving, you can always run it in a background thread (but be aware that this adds quite a bit of complexity and difficulty).如果 function 行为不端,您始终可以在后台线程中运行它(但请注意,这会增加相当多的复杂性和难度)。 Normally, you shouldn't need to sleep at all.通常,您根本不需要睡觉。 If you do have to sleep, then make it a loop that spins until enough time passes, processing the events from the OS and calling Sleep(0) which yields the timeslice.如果您确实必须休眠,则使其成为一个循环,直到足够的时间过去,处理来自操作系统的事件并调用产生时间片的Sleep(0)

Can you break your code into sections and use timers instead of Sleep?你能把你的代码分成几个部分并使用计时器而不是睡眠吗? That will keep the message pump running.这将使消息泵保持运行。 Look at this article in MSDN on using timers: http://msdn.microsoft.com/en-us/library/ms644901%28v=VS.85%29.aspx查看 MSDN 中关于使用计时器的这篇文章: http://msdn.microsoft.com/en-us/library/ms644901%28v=VS.85%29.aspx

Either offload the work to a separate thread (see CreateThread ) or, use PeekMessage et.要么将工作卸载到单独的线程(参见CreateThread ),要么使用PeekMessage等。 al in a tight loop to process window messages and keep the GUI alive. al 在一个紧密的循环中处理 window 消息并保持 GUI 活动。

Of course you should ask yourself whether you really need to sleep.当然,你应该问自己是否真的需要睡觉。 As you said, a sleep in the main thread (the thread that polls the message pump) prevents any GUI processing.正如您所说,主线程(轮询消息泵的线程)中的睡眠会阻止任何 GUI 处理。

You have to use threads.你必须使用线程。 Remember that if you have a long-running process in your GUI, you have to create a thread and let it deal with all of your code—which in this case contains many sleep instructions.请记住,如果您的 GUI 中有一个长时间运行的进程,您必须创建一个线程并让它处理您的所有代码——在这种情况下,它包含许多睡眠指令。 Once the thread finishes its job, it will notify you and you can then extract data from it.一旦线程完成其工作,它将通知您,然后您可以从中提取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM