Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OnIdle

0 views
Skip to first unread message

harvinder singh

unread,
Jun 18, 2003, 1:38:38 AM6/18/03
to
Hi,
I want to do the following.
After cetain period of inactivity by the user the Aplication should lock
and ask for userid/password.

How to check this find that user has not used the application for
certain period.

Thanks and Regards,
HArvinder Singh


Ajay Kalra

unread,
Jun 18, 2003, 3:14:29 AM6/18/03
to
You could install a WH_FOREGROUNDIDLE hook using SetWindowsHookEx(). This is
your gateway to find when the application is
idle. You can start the timer when you get the callback and kill it if an
activity(main message pump) occurs.

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


"harvinder singh" <i_har...@yahoo.ie> wrote in message
news:bcovlr$lppqc$1...@ID-195076.news.dfncis.de...

Roy Fine

unread,
Jun 18, 2003, 5:42:53 PM6/18/03
to

"harvinder singh" <i_har...@yahoo.ie> wrote in message news:bcovlr$lppqc$1...@ID-195076.news.dfncis.de...

HArvinder

Instead of hooking the WH_FOREGROUNDIDLE, I like to hook the mouse and keyboard - using WH_MOUSE and WH_KEYBOARD. This saves
having to start and kill a timer -- and it keeps the default message pump intact - and it minimizes the amount of processing on a
per message bases to a single arithmetic operation ( OK - an increment is three - load, inc, store - but certainly less than an
API call.)

It does involve creating a thread, and an event - but those are ONE TIME only events.

In an MFC SDI app, all of the work can be done in or near the CFrameWnd class - most of the work in the OnCreate handler.

Basically, the thread has a WaitForSingleObject, the object being the terminate notification event. On a time out, check to see
if there has been any mouse or keyboard activity since the last wait-for call. if not, then send message back to the CFrameWnd -
and do the timeout/noActivity processing there. In my example, I send the App Exit message -- you could use WN_APP+x and
implement a handler to do your own processing. In my example, my thread terrminates and the hooks are released when the idle
timeout period elapses -- if you want to do a screen saver, leave the thread and the hooks intact, and implement a two-state
handler - sending a start screensaver from the watching thread on noactivity, and an end screensaver from the kb/mouse hooks on
some activity.

The sample is pasted below... It just terminates an MFC SDI app after 25 seconds of no mouse or no keyboard activity. It would
be simple enough to activate a login dialog box in the frame window...

regards
roy fine

/* CMainFrame construction/destruction */
/* ---------------------------------------------*/
CMainFrame::~CMainFrame(){
DWORD sts;
sts = ::SetEvent(hEventRunning);
sts = ::WaitForSingleObject(hIdlWatchThrd,INFINITE);
::CloseHandle(hIdlWatchThrd);
}

/* ---------------------------------------------*/
LRESULT CALLBACK MyKBrdHookProc(int nCode, WPARAM wParam, LPARAM lParam){
idleCount++;
return CallNextHookEx(hKBrdHook,nCode,wParam, lParam);
}

/* ---------------------------------------------*/
LRESULT CALLBACK MyMouseHookProc(int nCode, WPARAM wParam, LPARAM lParam){
idleCount++;
return CallNextHookEx(hMouseHook,nCode,wParam, lParam);
}

/* ---------------------------------------------*/
DWORD WINAPI IdleWatchThreadProc(void *p){
DWORD sts;
int tval = idleCount-1;
while(1){
sts = ::WaitForSingleObject(hEventRunning,25000);
if(sts == WAIT_OBJECT_0) break;
if(tval == idleCount) {
sts = ::UnhookWindowsHookEx(hKBrdHook);
sts = ::UnhookWindowsHookEx(hMouseHook);
sts = ::PostMessage((HWND)p,WM_COMMAND,ID_APP_EXIT,0);
break;
}
else tval = idleCount;
}
return 0;
}


/* ---------------------------------------------*/
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){
CFrameWnd::OnCreate(lpCreateStruct);

m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndToolBar.LoadToolBar(IDR_MAINFRAME);

m_wndStatusBar.Create(this);
m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);

DWORD tid = ::GetCurrentThreadId();
hMouseHook = ::SetWindowsHookEx(WH_MOUSE,MyMouseHookProc,NULL,tid);
hKBrdHook = ::SetWindowsHookEx(WH_KEYBOARD,MyKBrdHookProc,NULL,tid);
DWORD thrdid;
hEventRunning = ::CreateEventW(NULL,FALSE,FALSE,L"88FBC62D-7D47-4f92-BCC9-0C3848A91B1D");
hIdlWatchThrd = ::CreateThread(NULL,0,IdleWatchThreadProc,this->m_hWnd,0,&thrdid);

return 0;
}

0 new messages