Solved

Tracking system idle time

Posted on 2004-03-26
4
317 Views
Last Modified: 2010-04-15
I am creating a component that has to do windows screen lock based on the system idle time.
Say if the system is idle for 15 mins it should lock the screen. How can I track the system idle time ?
Is there any readymade utility for this ?


Regards
Renjith
0
Comment
Question by:renjith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 10685640
Use GetLastInputInfo API.

The GetLastInputInfo function retrieves the time of the last input event.

BOOL GetLastInputInfo(
  PLASTINPUTINFO plii   // last input event
);

plii
[out] Pointer to a LASTINPUTINFO structure that receives the time of the last input event.

typedef struct tagLASTINPUTINFO {
  UINT cbSize;
  DWORD dwTime;
};

See also: GetTickCount API.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10686017
Take a look at this code:

using System;
using System.Runtime.InteropServices;

namespace Test1
{
    class Class1
    {
        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            public int cbSize;
            public int dwTime;
        }

        [DllImport ("User32.dll")]
        static extern void GetLastInputInfo(ref LASTINPUTINFO info);

        [DllImport ("Kernel32.dll")]
        public static extern int GetTickCount();

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            LASTINPUTINFO info = new LASTINPUTINFO();
            info.cbSize = Marshal.SizeOf(info);

            GetLastInputInfo(ref info);
            int n = GetTickCount();

            int nIdle = n - info.dwTime;

            Console.WriteLine("Idele time (ms): {0}", nIdle);
        }
    }
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question