Detect When Application Is Idle

I am looking for the best way to detect when a specific application is idle. In Task Manager there is the CPU column that indicates if a process is 0 or some other value. I want to know when a specific process is at 0 and remains in that state. I have a particular app that is always running in the background and should be consuming some CPU time all the time. For the most part it does, but occasionally it goes idle and I need to know when it does.
abncAsked:
Who is Participating?
 
JasonRawlinsConnect With a Mentor Commented:
Hello abnc,

Short answer:

As CalVerret mentioned, the most likely solution will be to handle the Application.Idle event. In your applications New() sub procedure, add the following code:

Public Sub New()
      AddHandler Application.Idle, AddressOf MyApplicationName_Idle
End Sub

Then create a method to handle this event:

Private Sub MyApplicationName_Idle(ByVal sender As Object, ByVal e As EventArgs)
      ' Perform appropriate tasks while the application is idle
End Sub

Explanation:

If your application is a Windows Forms application you can subscribe to the Application.Idle event. This can be done in the New() sub of the class. When the application finishes processing all of its tasks, it will automatically enter an idle state.

Reference to this event on MSDN2
http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.idle.aspx

There are other ways to manage processes on your computer as well. The following reference explains more about this:
http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.aspx

Description of how to use Events and Delegates
http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx

0
 
CarlVerretCommented:
Hi,

Have you ever tried to add and Handler on the Application.Idle event ?
0
 
abncAuthor Commented:
My console app is endeavoring to check the idle state of another app. That app is not one I have written so I cannot change the underlying code. I need to know when that app's process is idle using no CPU time.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
CarlVerretConnect With a Mentor Commented:
Hi again,

Is there any way that you could monitor this app with a PerformanceCounter object ?
0
 
abncAuthor Commented:
I have looked at the system diagnostics but don't see yet anyway to see what the current status of a specific process is regarding CPU time. I see I can get the TotalProcessorTime, but I want to know if that process is using any CPU time at a given moment. Obviously, Windows Task Manager is getting that data and showing it on the Processes Tab.

By way of information, the app I am checking shows up in the list of Applications in Windows Task Manager, but on random occasions it disappears from that list but is still listed among the processes. However, it is showing 00 CPU time and I need to know when it is in that state. Perhaps there is some way of checking for it among the list of applications rather than processes, although I would prefer to be able to check it's process current status.
0
 
JasonRawlinsCommented:
Hello, abnc

An application is always associated with a process. In this case, I think you want to monitor the process and take action based on its state.

In order to continually monitor the processes on your machine (similar to the task manager) you may need to create a Windows service. A Windows service is often used for long-running processes (such as continually monitoring processes) that don't interfere with other applications and users.

Look at http://msdn2.microsoft.com/en-us/library/d56de412(VS.80).aspx to see how you can create and set up a Windows service application.
0
 
abncAuthor Commented:
Several of these things I already had tried and is not the exact solution I was seeking. I finally came up with a method using part of what I had already tried and some of what was suggested.
0
All Courses

From novice to tech pro — start learning today.