简体   繁体   English

如何检查程序是否被使用?

[英]How to check whether the program is being used?

I need to close the application (C#) when user doesn't use it - let's say that when there is no Click event on any form of the program (there are about 100 forms). 我需要在用户不使用应用程序(C#)时关闭它-假设任何形式的程序(大约有100种形式)都没有Click事件。 Is there any way to do that without handling Click even on each form of the app. 有什么方法可以做到,甚至无需在应用程序的每种形式上都单击即可。 (I have the thread running each minute, where it could be checked)? (我每分钟运行一次线程,可以在哪里检查)? Thanks in advance! 提前致谢!

You can hook into the application message loop using the Application.AddMessageFilter function. 您可以使用Application.AddMessageFilter函数挂接到应用程序消息循环。 Write a message filter that inspects all mouse click messages and/or keyboard messages, or anything you're interested in. 编写一个消息过滤器,以检查所有鼠标单击消息和/或键盘消息或您感兴趣的任何内容。

For instance: 例如:

public class DetectActivityMessageFilter : IMessageFilter
{
    private const int WM_LBUTTONDOWN = 0x0201;

    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_LBUTTONDOWN)
        {
            // The left mouse button was pressed
        }

        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM