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
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
You can put a hook SetWindowsHookEx with WH_FOREGROUNDIDLE
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.
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