Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Idle Windows in C#

0.00/5 (No votes)
20 Apr 2016 1  
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:

        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:

    // 
    // 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:

    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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here