Click here to Skip to main content
15,888,279 members
Articles / Programming Languages / C#
Tip/Trick

Idle Windows in C#

Rate me:
Please Sign up or sign in to vote.
4.33/5 (6 votes)
20 Apr 2016CPOL 40.6K   1.1K   14   2
How to use Application.Idle event in C#

Introduction

Days ago, I was trying to implement a new functionality to lock user sessions in WinForm applications when a session has not been used by X minutes. That functionality is common in many software but I found few articles about this topic. For this reason, I decided to write this small article to share my experience using the Application.Idle event.

Background

The Idle event is invoked when the application finishes processing and is about to enter the idle state. How to know idle time of the application? This is the goal of this code.

Using the Code

In this code, I going to show, for me, the most simple way to resolve this question.

First step: Subscribe to the idle event:

C#
        public IdleSampleForm()
        {
            InitializeComponent();

            Application.Idle += Application_Idle;
            Application.ApplicationExit += Application_ApplicationExit;
        }

...........................

        private void Application_ApplicationExit(object sender, EventArgs e)
        {
            Application.Idle -= Application_Idle;
        }

Second step: Create timer as trigger to check the inactive elapsed time:

C#
    // 
    // timer1
    // 
    this.timer1.Interval = 1000;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

...........................

    private void timer1_Tick(object sender, EventArgs e)
    {
        _ignore_action = true;
    }

Third step: Implement idle event:

C#
    private void Application_Idle(object sender, EventArgs e)
    {
        //check if the user is logged or the application is locked
        if (LoginController.getInstance().OnlineUser != null && !_lockActive)
        {
            //capture current time
            DateTime _now = DateTime.Now;
            TimeSpan _timeSpan = _now - _last_time_idle;

..........................

            //check elapsed time
            if (_timeSpan.TotalMinutes > LoginController.TimeForIdleMin)
            {
                _lockActive = true;

                //lock application here

.......................................

                //Only I gonna to override _last_time_idle if timer does not 
                //cause an invocation of idle event
                if (!_ignore_action)
                {
                    _last_time_idle = DateTime.Now;
                }
            }

            //Always set to false on all idle invocation
            _ignore_action = false;
        }

Points of Interest

The timer always generates an invocation of idle event. For this reason, it is very important to use _ignore_action variable or another form to avoid set _last_time_idle by this invocation.

History

  • April 20th, 2016: Created

License

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


Written By
Software Developer
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

 
QuestionVery Interesting Pin
Sije de Haan22-Apr-16 21:23
Sije de Haan22-Apr-16 21:23 
PraiseThis is one of those things... Pin
James McCullough21-Apr-16 2:43
professionalJames McCullough21-Apr-16 2:43 

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.