• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2592
  • Last Modified:

Windows API: process handle, cpu usage, close message

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.

Thanks

Ref:
 Use the SendMessage API function:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/sendmessage.asp

Something like this:

SendMessage (hWnd, WM_Close, 0, 0);

To get the windw handle, use EnumWindows API function.

http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/enumwindows.asp
0
xinus_huy
Asked:
xinus_huy
  • 5
  • 3
1 Solution
 
cookreCommented:
There are several calls you can make to evaluate process activity:

GetProcessIoCounters()
GetProcessTimes()

GetThreadIoCounters()
GetThreadTimes()

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.
0
 
xinus_huyAuthor Commented:
hmm
with this, how can I say that a process is idle for 5 minutes ?
0
 
cookreCommented:
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
xinus_huyAuthor Commented:
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.

Thnx
0
 
cookreCommented:
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.

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

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

memset(&StartInfo,0,sizeof(StartInfo));
strcpy(CommandStr,"c:\\windows\\notepad.exe");
strcpy(WinSta0Default,"WinSta0\\Default");

StartInfo.cb=sizeof(StartInfo);
StartInfo.dwFlags=STARTF_USESHOWWINDOW;
StartInfo.wShowWindow=SW_SHOWMAXIMIZED;
StartInfo.lpDesktop=WinSta0Default;
if (0==CreateProcess(NULL,CommandStr,NULL,NULL,FALSE,0,NULL,NULL,&StartInfo,&ProcInfo))
   {
   sprintf(ts,"CreateProcess failed (%d)",GetLastError());
   PutMSG(ts);
   }
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);
      PutMSG(ts);
      // I'm too lazy to set up a timer for this example.
      Sleep(1000);
      }

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.
0
 
cookreCommented:
This shows code using SetWindowsHookEx() to capture mouse and keyboard events:

http://www.codeproject.com/dll/trackuseridle.asp
0
 
peterchen092700Commented:
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

0
 
xinus_huyAuthor Commented:
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
forever..........
I'll try the keyboard/mouse
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now