0

I want a simple method to check when the application is busy and when it is idle. After doing some searching I found two ways people have suggested. One us GetLastInputInfo function and other is the Application.Idle.

I just want to detect the Application inactivity not the system inactivity. So I am planning to use Application.Idle. But now how do I trigger an event when the application becomes active again? I am starting the timer in Idle event and I wish to reset that in the other function.

Any help would be appreciated.

My event handler:

void Application_Idle(object sender, EventArgs e)
{
    System.Timers.Timer aTimer = new System.Timers.Timer(5000);
    aTimer.Elapsed += aTimer_Elapsed;
    aTimer.Enabled = true;
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Fox
  • 9,384
  • 13
  • 42
  • 63
  • Thanks for the response .. I have edited my question .. That is how I am doing it .. I am stating a timer which is triggered after 5 sec .. Now I need to reset it if app becomes active in 2 sec .. – Fox Mar 11 '14 at 22:13
  • This code isn't ever going to get you anywhere, you also have to stop the timer. Use a plain Winforms Timer that you drop on the form. In the Idle event handler, first call the timer's Stop() method. Because the app just stopped being busy. Then call Start(). That's all you need. – Hans Passant Mar 11 '14 at 22:25
  • But I need to reset the timer when the user is active again. How do I check that? With start and stop, I will get the total amount of the time user has been inactive.. – Fox Mar 11 '14 at 22:46

1 Answers1

1

Consider using the IMessageFilter.PreFilterMessage method to look for a UI event that "wakes" the application from idle. Example below is from an application I wrote in VB, but principle is the same.

You can also filter the messages to determine what actions mean "wake", for instance does mouse over count? Or only mouse clicks and key presses?

Dim mf As New MessageFilter
Application.AddMessageFilter(mf)

...

Imports System.Security.Permissions

Public Class MessageFilter
    Implements IMessageFilter

    <SecurityPermission(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.UnmanagedCode)>
    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage

        ' optional code here determine which events mean wake

        ' code here to turn timer off

        ' return false to allow message to pass
        Return False

    End Function

End Class

Reference: https://msdn.microsoft.com/en-us/library/system.windows.forms.imessagefilter.prefiltermessage(v=vs.100).aspx

DenverJT
  • 125
  • 7