0

我想要一个简单的方法来检查应用程序何时忙碌以及何时空闲。在做了一些搜索后,我发现了人们建议的两种方法。一个是 GetLastInputInfo 函数,另一个是 Application.Idle。

我只想检测应用程序不活动而不是系统不活动。所以我打算使用Application.Idle。但是现在当应用程序再次激活时如何触发事件?我在空闲事件中启动计时器,我希望在其他功能中重置它。

任何帮助,将不胜感激。

我的事件处理程序:

void Application_Idle(object sender, EventArgs e)
{
    System.Timers.Timer aTimer = new System.Timers.Timer(5000);
    aTimer.Elapsed += aTimer_Elapsed;
    aTimer.Enabled = true;
}
4

1 回答 1

1

考虑使用 IMessageFilter.PreFilterMessage 方法来查找将应用程序从空闲状态“唤醒”的 UI 事件。下面的例子来自我用VB写的一个应用程序,但原理是一样的。

您还可以过滤消息以确定哪些操作意味着“唤醒”,例如鼠标悬停计数吗?还是只有鼠标点击和按键?

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

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

于 2015-12-02T16:02:24.417 回答