Windows如何检测挂窗? [英] how windows detects hung window ?

查看:48
本文介绍了Windows如何检测挂窗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在这里有点困惑,希望你能帮助我。


处理WM_POWERBROADCAST时

(wParam = PBT_APMQUERYSUSPEND),我必须在系统暂停或休眠之前做一些冗长的操作(30秒)。为了实现这一点,在我的消息处理程序中处理时,我创建了另一个线程,需要

来完成这个冗长的操作,并且当它完成时我将
设置事件设置为PBT_APMQUERYSUSPEND消息处理器(而

它在WaitForSinleObject中),该工作已经完成并且

现在可以允许windows暂停/休眠。问题是

当处理PBT_APMQUERYSUSPEND时,
WaitForSinleObject,windows不等我返回

值并继续暂停/休眠状态。奇怪的是,我不明白的是,如果我在我的PBT_APMQUERYSUSPEND处理程序中只调用MessageBox

(),则窗口等待

直到我按OK,即使我在10分钟后这样做了。


所以我不明白为什么,windows不认为我的帖子
$ b $当我调用MessageBox时b挂起来,并且当我有WaitForSingleObject时确实认为它挂了



我甚至尝试将PeekMessage删除所有

来自窗口队列的消息和呼叫睡眠(100),尝试

告诉窗口我没有挂,但它仍然恢复与

暂停/休眠。


另一件事是,文档说它将会等待20秒直到思考为止。我的线程挂起如果

我不从我的消息队列中删除WM_POWERBROADCAST,

但是我确实删除它并执行WaitForSingleObject,仍然它

继续暂停/休眠。


(在暂停/休眠之前我应该​​做的操作

是至关重要的,所以请不要建议其他相反的事情..)


希望有人可以帮助我。


谢谢

解决方案

< an ******* @ discussion.microsoft.com>在消息中写道

news:02 **************************** @ phx.gbl ... < blockquote class =post_quotes>在处理WM_POWERBROADCAST
(wParam = PBT_APMQUERYSUSPEND)时,我必须在系统暂停或休眠之前进行一些冗长的操作(30秒)。为了实现这一点,在我的消息处理程序处理PBT_APMQUERYSUSPEND时,我创建了另一个线程,它需要关注这个冗长的操作,当它完成时我将事件设置为PBT_APMQUERYSUSPEND消息处理器(当它在WaitForSinleObject中时),该工作已经完成,现在可以允许窗口暂停/休眠。问题是
当在WaitForSinleObject上处理PBT_APMQUERYSUSPEND时,windows不会等待我返回
值并继续暂停/休眠。


我不确定我明白你在做什么。如果你创建一个

后台线程只是为了在UI线程中同步等待它,那么在我看来你可能只是做了漫长的处理UI

线程。无论如何,这都不能解决你的问题。

奇怪的事情我不明白,如果我只在我的PBT_APMQUERYSUSPEND处理程序中调用MessageBox
() ,窗户等待
直到我按OK,即使我在10分钟后这样做。


Windows''启发式检测挂起​​的应用程序是发送一个无害的

WM_NULL消息。如果它没有得到回复(通常来自

DefWindowProc())那么它假定应用程序没有挂起。

所以我不明白为什么当我调用MessageBox时,windows不认为我的线程挂起了,当我有WaitForSingleObject时,确实认为它挂起了



这是因为MessageBox()旋转了一个消息循环,在显示对话框时调度消息



我甚至尝试将PeekMessage从窗口队列中删除所有
消息并调用Sleep(100),尝试告诉窗口我没有挂起,但它仍然以
暂停/休眠恢复。


Windows用来检测挂起的幸福感不是CPU利用率。

希望有人可以帮助我。




是的。而不是WaitForSingleObject(),在

循环中使用MsgWaitForMultipleObjects()。后台线程完成后退出循环。当

后台线程正在运行时,如果该函数指示你有一个

消息等待然后处理它,那么Windows知道你很忙。请仔细阅读该功能的

帮助条目,在开始下一次等待之前,必须排空消息队列




问候,



" William DePalo [MVP VC ++]" < WI *********** @ mvps.org>在消息中写道

news:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

是的。而不是WaitForSingleObject(),在
循环中使用MsgWaitForMultipleObjects()。后台线程完成后退出循环。当
后台线程正在运行时,如果该函数指示您有等待的消息然后处理它,那么Windows知道您正忙。仔细阅读
函数的帮助条目,你必须在开始下一次等待之前排空
消息的队列。




Will,


AtlWaitWithMessageLoop函数是否是这样做的一个很好的例子?

如果是这样的话,它是(可用的)内联提供的源代码。 />
-

Jeff Partch [VC ++ MVP]


查看SendMessageTimeout是否可以帮助您。我认为这就是windows内部如何计算

哪个进程挂起。


-

Ajay Kalra [MVP - VC ++ ]
aj*******@yahoo.com

<一个******* @ discussions.microsoft.com>在消息中写道

news:02 **************************** @ phx.gbl ...

|大家好,

|

|我在这里有点困惑,希望你能帮助我。

|

|在处理WM_POWERBROADCAST

|时(wParam = PBT_APMQUERYSUSPEND),我必须做一些冗长的

|系统暂停或休眠前的操作(30秒)。要

|实现这一点,在我的消息处理程序处理

|时PBT_APMQUERYSUSPEND,我创建另一个需要

|的线程做了这么长时间的操作,当它完成时我就是b $ b |将事件设置为PBT_APMQUERYSUSPEND消息处理器(而

|它在WaitForSinleObject中),该工作已完成并且

|现在可以允许windows挂起/休眠。问题是

|当在

|上处理PBT_APMQUERYSUSPEND时WaitForSinleObject,windows不等我回来

|值并暂停/休眠。很奇怪

|我不明白的是,如果我只拨打MessageBox

| ()在我的PBT_APMQUERYSUSPEND处理程序中,窗口等待

|直到我按OK,即使我在10分钟后这样做。

|

|所以我不明白为什么,windows不认为我的帖子

|我打电话给MessageBox时挂了,确实认为它挂了

|当我有WaitForSingleObject时。

|

|

|我甚至尝试将PeekMessage删除所有

|来自窗口队列的消息和调用Sleep(100),尝试

|告诉窗户我没挂,但它仍然以

|恢复暂停/休眠。

|

|另一件事是,文档说它将是b $ b |等待只有20秒,直到思考如果

|我的线程挂了我不会从我的消息队列中删除WM_POWERBROADCAST,

|但是我确实将它删除并执行WaitForSingleObject,它仍然是

|继续暂停/休眠。

|

| (在暂停/休眠之前我应该​​做的操作

|是至关重要的,所以请不要建议别的东西..)

|

|希望有人可以帮助我。

|

|谢谢

|


Hi All,

I am little confused here, hope you can help me.

While processing WM_POWERBROADCAST
(wParam=PBT_APMQUERYSUSPEND), I MUST to do some lengthy
operation(30 sec) before system Suspends or hibernates. To
achieve this, in my message handler when processing
PBT_APMQUERYSUSPEND, I create another thread which takes
care of making this lengthy operation, and when its done I
set event to PBT_APMQUERYSUSPEND message processor(while
it is in WaitForSinleObject), that work was finished and
now can allow windows to suspend/hibernate. The problem is
that when processing PBT_APMQUERYSUSPEND while on
WaitForSinleObject, windows doesn''t waits for me to return
value and proceeds with suspend/hibernation. The weird
thing I don''t understand is that if I call only MessageBox
() in my PBT_APMQUERYSUSPEND handler, the windows waits
until I press OK, even if I do so after 10 minutes.

So I don''t understand why, windows doesn''t think my thread
is hung when I call MessageBox and does think its hung
when I have WaitForSingleObject.
I even tried putting PeekMessage that removes all the
messages from window queue and call Sleep(100) , to try
tell windows that I am not hung, but it still resumes with
suspend/hibernation.

Another thing is that, documentation says that it will
wait only 20 seconds until "thinking" my thread is hung if
I don''t remove WM_POWERBROADCAST from my message queue,
but I do remove it and do WaitForSingleObject, still it
continues with suspend/hibernation.

(The operation that I should do before suspend/hibernate
is vital, so please don''t suggest other things instead..)

Hope some one can help me.

Thanks

解决方案

<an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...

While processing WM_POWERBROADCAST
(wParam=PBT_APMQUERYSUSPEND), I MUST to do some lengthy
operation(30 sec) before system Suspends or hibernates. To
achieve this, in my message handler when processing
PBT_APMQUERYSUSPEND, I create another thread which takes
care of making this lengthy operation, and when its done I
set event to PBT_APMQUERYSUSPEND message processor(while
it is in WaitForSinleObject), that work was finished and
now can allow windows to suspend/hibernate. The problem is
that when processing PBT_APMQUERYSUSPEND while on
WaitForSinleObject, windows doesn''t waits for me to return
value and proceeds with suspend/hibernation.
I''m not sure I understand what you are doing. If you are creating a
background thread only to wait on it synchronously in the UI thread, it
seems to me that you might as well just do the lengthy processing the the UI
thread. No matter, that won''t solve your problem.
The weird
thing I don''t understand is that if I call only MessageBox
() in my PBT_APMQUERYSUSPEND handler, the windows waits
until I press OK, even if I do so after 10 minutes.
Windows'' heuristic to detect a hung application is to send it an innocuous
WM_NULL message. If it does not get back a reply (most often from with
DefWindowProc() ) then it assumes the application is not hung.
So I don''t understand why, windows doesn''t think my thread
is hung when I call MessageBox and does think its hung
when I have WaitForSingleObject.
That''s because MessageBox() spins a message loop, dispatching messages while
the dialog is displayed.
I even tried putting PeekMessage that removes all the
messages from window queue and call Sleep(100) , to try
tell windows that I am not hung, but it still resumes with
suspend/hibernation.
It is not CPU utilization that Windows uses to detect a hung happlication.
Hope some one can help me.



Yes. Instead of WaitForSingleObject(), use MsgWaitForMultipleObjects() in a
loop. When your background thread completes exit the loop. While the
background thread is running, if the function indicates that you have a
message waiting then process it so Windows knows that you are busy. Read the
help entry for the function carefully, you must drain the queue of messages
before you begin the next wait.

Regards,
Will


"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Yes. Instead of WaitForSingleObject(), use MsgWaitForMultipleObjects() in a loop. When your background thread completes exit the loop. While the
background thread is running, if the function indicates that you have a
message waiting then process it so Windows knows that you are busy. Read the help entry for the function carefully, you must drain the queue of messages before you begin the next wait.



Will,

Would the AtlWaitWithMessageLoop function be a good example of doing this?
If so, it''s (available as) an inline with source provided.
--
Jeff Partch [VC++ MVP]


See if SendMessageTimeout helps you. I think this is how windows internally figures out
which process is hung.

--
Ajay Kalra [MVP - VC++]
aj*******@yahoo.com
<an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
| Hi All,
|
| I am little confused here, hope you can help me.
|
| While processing WM_POWERBROADCAST
| (wParam=PBT_APMQUERYSUSPEND), I MUST to do some lengthy
| operation(30 sec) before system Suspends or hibernates. To
| achieve this, in my message handler when processing
| PBT_APMQUERYSUSPEND, I create another thread which takes
| care of making this lengthy operation, and when its done I
| set event to PBT_APMQUERYSUSPEND message processor(while
| it is in WaitForSinleObject), that work was finished and
| now can allow windows to suspend/hibernate. The problem is
| that when processing PBT_APMQUERYSUSPEND while on
| WaitForSinleObject, windows doesn''t waits for me to return
| value and proceeds with suspend/hibernation. The weird
| thing I don''t understand is that if I call only MessageBox
| () in my PBT_APMQUERYSUSPEND handler, the windows waits
| until I press OK, even if I do so after 10 minutes.
|
| So I don''t understand why, windows doesn''t think my thread
| is hung when I call MessageBox and does think its hung
| when I have WaitForSingleObject.
|
|
| I even tried putting PeekMessage that removes all the
| messages from window queue and call Sleep(100) , to try
| tell windows that I am not hung, but it still resumes with
| suspend/hibernation.
|
| Another thing is that, documentation says that it will
| wait only 20 seconds until "thinking" my thread is hung if
| I don''t remove WM_POWERBROADCAST from my message queue,
| but I do remove it and do WaitForSingleObject, still it
| continues with suspend/hibernation.
|
| (The operation that I should do before suspend/hibernate
| is vital, so please don''t suggest other things instead..)
|
| Hope some one can help me.
|
| Thanks
|


这篇关于Windows如何检测挂窗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆