How to detect a Winforms app has been idle for certain amount of time

后端 未结 2 1559
孤城傲影
孤城傲影 2020-12-29 12:05

What is the best way, to detect if a C# Winforms application has been idle for a certain period of times?

If a user decides to ALT+TAB and do some work with Microsof

2条回答
  •  醉梦人生
    2020-12-29 12:12

    You would need to decide what "idle" means. I assume no mouse or key input received by the application during the required time.

    You could add a message filter that will see all application-wide Windows messages. See Application.AddMessageFilter. Check for the message codes for key down and mouse down, and record the last time that they occur, but return false so that all messages are handled as normal by the runtime. Likely codes are:

    WM_LBUTTONDOWN = 513
    RBUTTONDOWN = 516
    WM_KEYDOWN = 256
    

    Then in a separate timer elsewhere check when the last event is more than 30 minutes ago.

    There are also potential issues with "terminating" the application. If the application has modal forms open, it sounds as if it might not be in a particularly safe state to kill abruptly.

提交回复
热议问题