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
Solved

Tracking system idle time

Posted on 2004-03-26
4
311 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS subreport with parameter 3 47
SqlDataBase 7 53
ASP.NET(C#) Eliminating weekends from a date range 2 45
VB.NET 2008 - SQL Timeout 9 34
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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