Windows API: process handle, cpu usage, close message

Posted on 2004-11-09
Last Modified: 2013-11-13
I've close my question to soon.
OK So I can get the handle and send a close message.
How can I see if the process is idle or not ?
In fact how can I loop and see if it is idle for 5 minutes, and close if more without blocking everything.


 Use the SendMessage API function:

Something like this:

SendMessage (hWnd, WM_Close, 0, 0);

To get the windw handle, use EnumWindows API function.
Question by:xinus_huy
    LVL 23

    Expert Comment

    There are several calls you can make to evaluate process activity:



    To be complete, one should also check for TCP and UDP traffic, but I don't know how to get process specific counters for those.
    LVL 1

    Author Comment

    with this, how can I say that a process is idle for 5 minutes ?
    LVL 23

    Expert Comment

    Capture the current counters for the target process.
    Set an X minute timer.
    When the timer fires, if the the counters are the same, you can consider the process idle or hung.
    If any of the counters has changed, the target process is still active, so capture the current counters for comparing when the timer next fires.
    LVL 1

    Author Comment

    When I use GetProcessTimes() the wHour, wMinute and wSecond is always the same.
    and wYear is 1601 ?!!!

    It never change.....

    Do the counters (GetProcessIoCounters)  change when the user is only using keyboard or mouse click ?
    Or is it IO for disk access ?

    The application I want to close is supposed to be idle when the user does not click or type even.
    Or maybe is it better to check CPU usage ?

    I increase the points if you can give me some code cuz I need to finish this program soon and do not want to
    spend too much time on the msdn.

    LVL 23

    Expert Comment

    The IO counts are just for disk IO.

    To monitor keyboard and mouse events, use SetWindowsHookEx().

    I'll put together some code for process times and post it later (possibly tonight (it's ~20:00 now (I'm GMT-5)), but maybe not 'till tomorrow.

    LVL 23

    Expert Comment

    I put the following in a non-interactive service (hence, the reference to WinSta0):

    STARTUPINFO          StartInfo;
    char                 WinSta0Default[512];
    char                 CommandStr[512];


    if (0==CreateProcess(NULL,CommandStr,NULL,NULL,FALSE,0,NULL,NULL,&StartInfo,&ProcInfo))
       sprintf(ts,"CreateProcess failed (%d)",GetLastError());
    HANDLE hPID=ProcInfo.hProcess;
    FILETIME CreationTime;
    FILETIME ExitTime;
    FILETIME KernelTime;
    FILETIME UserTime;

    while (GetProcessTimes(hPID,&CreationTime,&ExitTime,&KernelTime,&UserTime))
          // One second is only 10^6, so lazy me will just look at the low DWORD
          sprintf(ts,"Kernal Time:  %9ld    User Time: %9ld",KernelTime.dwLowDateTime,UserTime.dwLowDateTime);
          // I'm too lazy to set up a timer for this example.

    and got the following results:

    20:21:14.738 - Kernal Time:          0    User Time:         0
    20:21:15.750 - Kernal Time:          0    User Time:    200288
    20:21:16.751 - Kernal Time:          0    User Time:    200288
    20:21:17.753 - Kernal Time:          0    User Time:    200288
    20:21:18.754 - Kernal Time:          0    User Time:    200288
    20:21:19.756 - Kernal Time:          0    User Time:    200288
    20:21:20.777 - Kernal Time:     300432    User Time:    200288
    20:21:21.789 - Kernal Time:    1001440    User Time:    701008
    20:22:26.291 - Kernal Time:    2904176    User Time: 202591312
    20:22:27.313 - Kernal Time:    2904176    User Time: 212705856
    20:22:28.324 - Kernal Time:    2904176    User Time: 222519968
    20:22:29.336 - Kernal Time:    2904176    User Time: 232434224
    20:22:30.347 - Kernal Time:    2904176    User Time: 234937824
    20:22:31.359 - Kernal Time:    2904176    User Time: 234937824
    20:22:32.380 - Kernal Time:    2904176    User Time: 234937824

    Note that for kernel and user times, the FILETIME is not a real time, but just a 64-bit counter of 100ns intervals consumed.
    LVL 23

    Accepted Solution

    This shows code using SetWindowsHookEx() to capture mouse and keyboard events:
    LVL 7

    Expert Comment

    Other options:
    If you just want to find out if the process is responding to Windows messages, you can use SendMessageTimeout.
    WaitForInputIdle waits (with a specified timeout) if a process is ready to process input

    LVL 1

    Author Comment

    By the way your code with getprocesstimes is printing
    Kernel Time: 1093750   User Time: 156250
    Kernel Time: 1093750   User Time: 156250
    Kernel Time: 1093750   User Time: 156250
    Kernel Time: 1093750   User Time: 156250
    Kernel Time: 1093750   User Time: 156250
    Kernel Time: 1093750   User Time: 156250
    I'll try the keyboard/mouse

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    bigDiff challenge 17 58
    map interface methods 3 46
    EvenOdd challenge 10 68
    drawing animated level bar based on numbers 3 44
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now