How do I determine how long the PC has been inactive for?

So you know how antiviruses tend to scan the computer if nobody has been using the PC for x amount of minutes?

On Rust how do I determine how long Windows has been inactive for?

The first step is to figure out how to do it in C, and then use the winapi crate or the new fancy windows crate to call appropriate Windows system calls.


Not 100% sure how antivirus tools do it, but a guess is that they kick off when a user's session was switched away from (like when the workstation was locked).

Possibly you can combine one of the crates @kornel mentioned with this: WM_WTSSESSION_CHANGE message (Winuser.h) - Win32 apps | Microsoft Docs

(It's confusing to me that those things live under "terminal services", but apparently it's not exclusively for terminal services).

I did some work with authentication modules in Windows many years ago, and I seem to recall that we used a "Workstation lock/unlock" notification, but I suspect that notification was reserved for the authentication system so it's probably not trivially available to regular applications.

1 Like

The difference between GetTickCount() and dwTime from GetLastInputInfo() looks like what you’re asking for.

Depending on what you’re doing, you might be better off with a Task Scheduler task that's defined to run a program on idle, which will let the OS use its knowledge of battery and power state to decide whether it’s a good time to run your program.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.