A simple tools to check if user is idle or not.
P/Invoke (Platform Invocation Services) is utilized to invoke functions from native Windows libraries directly within managed C# code. Specifically, it's used to call the GetLastInputInfo and GetLastError functions from the User32.dll and Kernel32.dll libraries, respectively.
- The
LASTINPUTINFO
structure is defined to store information about the last input event received by the system. It contains two members:cbSize
: Represents the size of the structure in bytes.dwTime
: Holds the time of the last input event, measured in milliseconds.
- Two functions from the Windows API are imported using Platform Invocation Services (P/Invoke):
GetLastInputInfo
: Retrieves information about the last input event.GetLastError
: Retrieves the last-error code value for the calling thread.
- This method retrieves the amount of time that has elapsed since the last user input event.
- It creates an instance of the
LASTINPUTINFO
struct, initializes itscbSize
, and callsGetLastInputInfo
to populate the struct with the last input information. - The method calculates the idle time by subtracting the time of the last input event from the current system time (
Environment.TickCount
) and returns the result in milliseconds.
- This method retrieves the time (in milliseconds) of the last input event.
- It follows a similar process as the
GetIdleTime
method, creating an instance ofLASTINPUTINFO
, initializing itscbSize
, and callingGetLastInputInfo
to populate the struct. - If
GetLastInputInfo
fails, it throws an exception, providing the error code returned byGetLastError
. - Finally, it returns the time of the last input event stored in the
dwTime
member of theLASTINPUTINFO
struct.
- This method checks if the system is currently idle by calling the
GetLastInputTime
method. - If the time of the last input event is not zero, it indicates that the system is not idle, and the method returns
false
. - If the time is zero, it signifies that the system is idle, and the method returns
true
.
- You can use the
IdleHandler
class to monitor user activity and implement features like auto-locking screens after a period of inactivity, tracking user session duration, or triggering background tasks based on user idle time.
You can just the function like this:
// Set the idle threshold to 5 seconds
const int idleThreshold = 5 * 1000;
if (IdleHandler.IsIdle() && IdleHandler.GetIdleTime() > idleThreshold)
{
Console.WriteLine("User is idle");
// Do something
}
else
{
Console.WriteLine("Active");
// Do something
}