DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. How to Detect if a User Is Idle

How to Detect if a User Is Idle

In this article, look at how to detect if a user is active on a Windows machine in order to help deliver notifications more effectively. Read on to find out more.

Romiko Derbynew user avatar by
Romiko Derbynew
·
Oct. 28, 16 · Tutorial
Like (2)
Save
Tweet
Share
7.89K Views

Join the DZone community and get the full member experience.

Join For Free

You are running a Windows forms application that runs as a system tray. You have a few notifications that you would like to show the user. However, what good are notifications if the user is away? He or she will not see them.

Solution

Run a timer that detects when the user is active on the machine, and then show the notification or other tasks that you would like to provide. Below is the sample code that will do this for you. Of course, for your production environment, you would use a timer of some sort or event subscription service.

I have tested this by using other applications while the program monitors my input. It's safe to say that it works across all of my applications, even when the screen is locked. You might want to deal with an issue where the screen is locked but the user is moving the mouse.

I know that MSDN mentions that GetLastInputInfo is not system wide. However, on my Windows 10 machine, it does seem to be the case that it is system-wide.

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace MyMonitor
{
 class Program
 {
 static void Main()
 {

 for (var i = 0; i < 1000; i++)
 {
 Console.WriteLine($"Last Input: {LastInput.ToShortTimeString()}");
 Console.WriteLine($"Idle for: {IdleTime.Seconds} Seconds");
 Thread.Sleep(TimeSpan.FromSeconds(1));
 }
 }

 [DllImport("user32.dll", SetLastError = false)]
 private static extern bool GetLastInputInfo(ref Lastinputinfo plii);
 private static readonly DateTime SystemStartup = DateTime.Now.AddMilliseconds(-Environment.TickCount);

 [StructLayout(LayoutKind.Sequential)]
 private struct Lastinputinfo
 {
 public uint cbSize;
 public readonly int dwTime;
 }

 public static DateTime LastInput => SystemStartup.AddMilliseconds(LastInputTicks);

 public static TimeSpan IdleTime => DateTime.Now.Subtract(LastInput);

 private static int LastInputTicks
 {
 get
 {
 var lii = new Lastinputinfo {cbSize = (uint) Marshal.SizeOf(typeof(Lastinputinfo))};
 GetLastInputInfo(ref lii);
 return lii.dwTime;
 }
 }
 }
}

idle-time-of-user

IDLE (Python) application Windows Forms Machine Production (computer science) Monitor (synchronization) IT

Published at DZone with permission of Romiko Derbynew, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Upgrade Guide To Spring Data Elasticsearch 5.0
  • Mr. Over, the Engineer [Comic]
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • Educating the Next Generation of Cloud Engineers With Google Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: