Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / C#

Auto-logout Within a Windows Application

Rate me:
Please Sign up or sign in to vote.
4.29/5 (11 votes)
18 Jan 2008CPOL2 min read 87.2K   3K   45   28
Simple, encapsulated component for detecting whether a Windows Forms application is active or idle.

Introduction

Please remember that this code has not been super heavily tested, and may contain bugs. Also, I am a bit of a newbie when it comes to programming with Windows messages, so please don't be overly harsh.

Background

Recently, I was asked to add an "auto-logout" type feature to an existing Windows desktop application. It didn't seem like a big deal, but when I sat down to think about it, I realized that it would be quite a bit of work. The reason for this is that the desktop application in question has many different modal forms and each form is significantly different from all the others, and... the point is that it would have been incredibly messy to try to do some ad-hoc event handling of all events that would make us consider the application "active".

Early on in my research, I found a suggestion to look into Windows hooks. I didn't know a lot about hooks (and I still don't, honestly), so it seemed a little intimidating. But I found a few good reference implementations, pieced them together, and did a little bit of tweaking. I guess now would be a good time to document those code snippets I got:

  • Even though I didn't use low-level hooks, the code I found on Steven Toub's blog was useful.
  • Also, I got quite a bit from a Windows KB page.

Using the Code

The activity monitor object is a disposable object that can be declared in whatever scope you would like. In the attached demo application, it is declared as a member of the main form:

C#
ActivityMonitor.ActivityMonitor _am = new ActivityMonitor.ActivityMonitor();

Then, within the constructor, it is initialized:

C#
_am.WarningMinutes = 0.9;
_am.MaxMinutesIdle = 1;
_am.Idle += new EventHandler(am_Idle); 

...where am_Idle is the handler for when the application becomes idle:

C#
void am_Idle(object sender, EventArgs e)
{
    Application.Exit();
}

Points of Interest

One thing that I did have some trouble with was that my little component was intercepting Windows mouse messages even when the mouse wasn't doing anything (not moving or clicking). I finally noticed that they seemed to be firing when the mouse was over my form. To me that doesn't count as being active, so I worked around this by only intercepting "clicking" messages:

C#
private int MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
    MouseMessages mouseInfo = (MouseMessages)wParam;

    if (nCode >= 0 &&
       ((mouseInfo == MouseMessages.WM_LBUTTONDOWN) || (
           mouseInfo == MouseMessages.WM_RBUTTONDOWN)))
    _lastActivity = DateTime.Now;
        
    return CallNextHookEx(_mouseHookID, nCode, wParam, lParam); 
}

Anyway, I'd appreciate any feedback, and I hope someone can find this to be of some use.

History

  • 14 January, 2008 -- Original version posted.
  • 18 January, 2008 -- Downloads updated.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMultiple popup Auto log out popup coming Pin
rajainrahul28-Jul-20 21:28
rajainrahul28-Jul-20 21:28 
QuestionWhere is the handler linked to the Idle event? Pin
Spasticus8-Dec-10 22:40
Spasticus8-Dec-10 22:40 
AnswerRe: Where is the handler linked to the Idle event? Pin
Spasticus9-Dec-10 0:22
Spasticus9-Dec-10 0:22 
GeneralMy vote of 4 Pin
Spasticus8-Dec-10 22:34
Spasticus8-Dec-10 22:34 
GeneralVEry imp Pin
arjun_2219-Sep-10 21:53
arjun_2219-Sep-10 21:53 
GeneralMy vote of 5 Pin
M_pareek10-Aug-10 21:21
M_pareek10-Aug-10 21:21 
GeneralRe: My vote of 5 Pin
Member 107477553-Jul-14 4:36
Member 107477553-Jul-14 4:36 
GeneralMy vote of 4 Pin
Spasticus11-Jul-10 10:24
Spasticus11-Jul-10 10:24 
GeneralUseful article Pin
Jagadeesh HS19-Jun-08 20:13
Jagadeesh HS19-Jun-08 20:13 
GeneralRe: Useful article Pin
chiznatworth20-Jun-08 3:36
chiznatworth20-Jun-08 3:36 
GeneralThanks!!! - Also, you can use a MessageFilter instead of the Hooks Pin
JihedHalimi2-May-08 5:11
JihedHalimi2-May-08 5:11 
GeneralRe: Thanks!!! - Also, you can use a MessageFilter instead of the Hooks Pin
Member 367729923-Mar-10 6:21
Member 367729923-Mar-10 6:21 
Generaldetect mouse over app Pin
doctrane18-Jan-08 6:06
doctrane18-Jan-08 6:06 
GeneralRe: detect mouse over app Pin
chiznatworth18-Jan-08 8:31
chiznatworth18-Jan-08 8:31 
GeneralGetLastInputInfo Pin
Willem Fourie18-Jan-08 2:55
Willem Fourie18-Jan-08 2:55 
GeneralRe: GetLastInputInfo Pin
chiznatworth18-Jan-08 3:10
chiznatworth18-Jan-08 3:10 
GeneralRe: GetLastInputInfo Pin
chiznatworth18-Jan-08 3:42
chiznatworth18-Jan-08 3:42 
GeneralSource is still not available. Pin
Ashaman17-Jan-08 1:33
Ashaman17-Jan-08 1:33 
Can you post it somewhere else and send a link?
GeneralRe: Source is still not available. Pin
chiznatworth17-Jan-08 2:58
chiznatworth17-Jan-08 2:58 
GeneralModal and Modeless Dialogs Pin
Chris Meech15-Jan-08 10:05
Chris Meech15-Jan-08 10:05 
GeneralRe: Modal and Modeless Dialogs Pin
chiznatworth15-Jan-08 11:30
chiznatworth15-Jan-08 11:30 
GeneralRe: Modal and Modeless Dialogs Pin
Chris Meech16-Jan-08 3:34
Chris Meech16-Jan-08 3:34 
GeneralI'd be interested in the code Pin
Bert delaVega15-Jan-08 6:56
Bert delaVega15-Jan-08 6:56 
GeneralRe: I'd be interested in the code Pin
chiznatworth15-Jan-08 9:01
chiznatworth15-Jan-08 9:01 
GeneralRe: I'd be interested in the code Pin
Member 367729923-Mar-10 7:06
Member 367729923-Mar-10 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.