Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I need to know how long a Windows program has been running, and how long has it been running without the user interacting with it.

The point is to automatically close the program if the user is not using it.

Thanks.

What I have tried:

I searched for a Windows API function that gaves me that information, but I didn't find any.
Posted
Updated 21-Mar-17 2:27am

To get the run time of a Windows application you can use the GetProcessTimes function (Windows)[^] passing the process handle (GetCurrentProcess function (Windows)[^]). To get the run time subtract the lpCreationTime from the current time.

With C/C++ you can also use the clock[^] function.

To know the last time of user interaction you have to get and store the time at each user input. How to implement this depends on the kind of your application. One option is to hook keyboard and mouse events using the SetWindowsHookEx function (Windows)[^]. See also Hooks Overview (Windows)[^].
 
Share this answer
 
I'd start with the Process API - there is information on various 'times' start time etc returned :- Process Class (System.Diagnostics)[^]

Look at StartTime, TotalProcessorTime, UserProcessorTime and see if any/all of those meet your needs
 
Share this answer
 
Thanks for your answers, Jochen and Garth.

The program that I need to control is not my application, it's a comercial program. I yet found the GetProcessTimes, but this function does not return the time that I need.

Neither found the solution with the Process Class.

The solution can be to use one of these programs:

Idle Software Closer - Automatically Close Programs Concurrent License[^]

Auto Close Idle Programs, Accounting, Financial, and Database applications[^]

One solution to try to do it myself can be to check continuously if the program is idle or not, and control with a timer when I detect that it is idle. But, as far as I checked, none of both solutions returns if the Windows program is idle or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900