Fox
Fox

Reputation: 9444

Detect the application active time (after Application.Idle)

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;
}

Upvotes: 0

Views: 707

Answers (1)

DenverJT
DenverJT

Reputation: 125

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

Upvotes: 1

Related Questions