检测进程是否空闲

发布于 2024-11-28 04:31:27 字数 88 浏览 1 评论 0原文

有没有办法检测Windows进程是否空闲?

空闲 意味着当特定应用程序的进程没有处理任何内容时(应用程序正在等待用户输入)。

干杯

Is there a way to detect whether a Windows process is idle?

idle Means, when a particular application's process is not processing anything(The application is waiting for userinput).

cheers

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

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

发布评论

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

评论(3

诗笺 2024-12-05 04:31:27

您可以使用 SetWindowsHookEx 放置一个钩子WH_FOREGROUND空闲

You can put a hook SetWindowsHookEx with WH_FOREGROUNDIDLE

往事随风而去 2024-12-05 04:31:27

我在 MFC 案例中发现的有关此主题的简短摘要,在这种情况下,您希望在进程空闲时收到通知(做一些后台工作等),但不是以轮询/等待方式

(变量名称是建议):

-> ;如果它是一个没有模式对话框的 MFC 应用程序:

add ON_MESSAGE_VOID(WM_IDLEUPDATECMDUI,OnIdleUpdateCmdUI) to message map together
with the method afx_msg void OnIdleUpdateCmdUI()

->如果它是 MFC 应用程序中的对话框:

add ON_MESSAGE(WM_KICKIDLE, OnKickIdle) to message map together 
with the method afx_msg LRESULT OnKickIdle(WPARAM wParam, LPARAM lParam);

->如果您想要两者(应用程序和对话框):

.) 将公共成员变量添加到主框架(也可以是静态全局变量)

HOOKPROC m_detectIdleHook

.) 将方法原型添加到主框架的头文件中,

friend LRESULT CALLBACK OnForeGroundIdle( int nCode, WPARAM wParam, LPARAM lParam )

并包含此内容(请注意,它是不是主框架的成员函数!)

LRESULT CALLBACK OnForeGroundIdle( int nCode, WPARAM wParam, LPARAM lParam )
{   
    // Do/check stuff in idle time here
    return ::CallNextHookEx( (HHOOK)((CMyMainFrame*)AfxGetMainWnd())->m_detectIdleHook, nCode, wParam, lParam );
}

。)在主框架的末尾设置窗口挂钩 ::OnCreate

m_detectIdleHook = (HOOKPROC)SetWindowsHookEx( WH_FOREGROUNDIDLE, 
                                         OnForeGroundIdle,
                                         NULL,
                                         ::GetCurrentThreadId());

。)最后,当您完成后,在主框架解构函数中取消挂钩

UnhookWindowsHookEx((HHOOK)m_detectIdleHook);

这些解决方案都不起作用如果应用程序是不活动(另一个应用程序具有焦点)。我在这里看到的唯一解决方案是使用 WM_TIMER 并通过 WaitForInputIdle 检查空闲状态(http://msdn.microsoft.com/en-us/library/ms687022%28VS.85%29.aspx),但这会引入一定的轮询间隔可靠性。

资料来源:

-本页

-http://www.drdobbs.com/184416462

-http://www.codeguru.com/forum/showthread.php?t=199148

-http://www.codeproject.com/KB/对话框/idledialog.aspx?msg=770930

A short summary of what I found about this topic for the MFC case where you want to be notified whenever the process is idle (do some background work etc.) but not in a polling/ waiting fashion

(variable names are suggestions):

-> If its an MFC application without modal dialogs:

add ON_MESSAGE_VOID(WM_IDLEUPDATECMDUI,OnIdleUpdateCmdUI) to message map together
with the method afx_msg void OnIdleUpdateCmdUI()

-> If its a dialog within a MFC application:

add ON_MESSAGE(WM_KICKIDLE, OnKickIdle) to message map together 
with the method afx_msg LRESULT OnKickIdle(WPARAM wParam, LPARAM lParam);

-> If you want both (application & dialogs):

.) add a public member variable to the main frame (also a static global variable is possible)

HOOKPROC m_detectIdleHook

.) add method prototype to the main frame's header file

friend LRESULT CALLBACK OnForeGroundIdle( int nCode, WPARAM wParam, LPARAM lParam )

with this content (note that it is not a member function of the main frame!)

LRESULT CALLBACK OnForeGroundIdle( int nCode, WPARAM wParam, LPARAM lParam )
{   
    // Do/check stuff in idle time here
    return ::CallNextHookEx( (HHOOK)((CMyMainFrame*)AfxGetMainWnd())->m_detectIdleHook, nCode, wParam, lParam );
}

.) set the window hook at the end of the main frame ::OnCreate

m_detectIdleHook = (HOOKPROC)SetWindowsHookEx( WH_FOREGROUNDIDLE, 
                                         OnForeGroundIdle,
                                         NULL,
                                         ::GetCurrentThreadId());

.) At the end when you are finished, unhook the window in the main frame deconstructor

UnhookWindowsHookEx((HHOOK)m_detectIdleHook);

None of these solutions will work if the application is not active (another application has the focus). The only solution I see here is to use a WM_TIMER and to check the idle state by WaitForInputIdle (http://msdn.microsoft.com/en-us/library/ms687022%28VS.85%29.aspx) but this would introduce a certain polling interval dependability.

Sources:

-This page

-http://www.drdobbs.com/184416462

-http://www.codeguru.com/forum/showthread.php?t=199148

-http://www.codeproject.com/KB/dialog/idledialog.aspx?msg=770930

东京女 2024-12-05 04:31:27

如果您的意思是要检测应用程序是否正常并接收消息(用户输入),请检查此函数的返回:

SendMessageTimeout(HwndInQuestion, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 10);

只需将超时(示例中为 10 毫秒)设置为您认为对您的使用合理的值即可。

If you mean you want to detect if the application is happy and receiving messages (user input) check the return of this function:

SendMessageTimeout(HwndInQuestion, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 10);

Just set the timeout (10ms in the example) to what you think is sensible for your use.

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