如何确定C#应用程序中的空闲时间? [英] How do I determine idle time in my C# application?

查看:59
本文介绍了如何确定C#应用程序中的空闲时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序中获取Windows空闲时间.我正是使用此代码:

I want to get the Windows Idle Time from my application. I am using exactly this code:

http://dataerror.blogspot.de/2005/02/detect-windows-idle-time.html

我已经在Windows 7上对此进行了测试,并且可以正常工作,但是在Windows 8上却只有零.

I have tested this on Windows 7 and it is working properly, but I am getting only a zero on Windows 8.

有什么办法解决这个问题吗?

Any ideas how to fix this?

推荐答案

我对您的方法采用了稍微不同的方法...这确定了应用程序的空闲时间,而不是使用系统范围的空闲时间.我不确定这是否满足您的需求,但是可能会帮助您进一步发展.它还具有纯.NET的优势,而不是使用 DllImport .

I took a slightly different approach to your method... This determines the idle time for your application, rather than using the system-wide idle time. I'm not sure if this meets your needs, however it might help you get further. It also has the benefit of being pure .NET instead of usingDllImport.

public partial class MyForm : Form, IMessageFilter {

    private Timer _timer;

    // we only need one of these methods...
    private DateTime _wentIdle;
    private int _idleTicks;

    public MyForm() {

        // watch for idle events and any message that might break idle
        Application.Idle += new EventHandler(Application_OnIdle);
        Application.AddMessageFilter(this);

        // use a simple timer to watch for the idle state
        _timer = new Timer();
        _timer.Tick += new EventHandler(Timer_Exipred);
        _timer.Interval = 1000;
        _timer.Start();

        InitializeComponent();
    }

    private void Timer_Exipred(object sender, EventArgs e) {
        TimeSpan diff = DateTime.Now - _wentIdle;

        // see if we have been idle longer than our configured value
        if (diff.TotalSeconds >= Settings.Default.IdleTimeout_Sec) {
            _statusLbl.Text = "We Are IDLE! - " + _wentIdle;
        }

        /**  OR  **/

        // see if we have gone idle based on our configured value
        if (++_idleTicks >= Settings.Default.IdleTimeout_Sec) {
            _statusLbl.Text = "We Are IDLE! - " + _idleTicks;
        }
    }

    private void Application_OnIdle(object sender, EventArgs e) {
        // keep track of the last time we went idle
        _wentIdle = DateTime.Now;
    }

    public bool PreFilterMessage(ref Message m) {
        // reset our last idle time if the message was user input
        if (isUserInput(m)) {
            _wentIdle = DateTime.MaxValue;
            _idleTicks = 0;

            _statusLbl.Text = "We Are NOT idle!";
        }

        return false;
    }

    private bool isUserInput(Message m) {
        // look for any message that was the result of user input
        if (m.Msg == 0x200) { return true; } // WM_MOUSEMOVE
        if (m.Msg == 0x020A) { return true; } // WM_MOUSEWHEEL
        if (m.Msg == 0x100) { return true; } // WM_KEYDOWN
        if (m.Msg == 0x101) { return true; } // WM_KEYUP

        // ... etc

        return false;
    }
}

我确实有两种方法可以确定空闲状态:一种使用 DateTime 对象,另一种使用简单的计数器.您可能会发现其中之一更适合您的需求.

I really have two methods for determining idle here... One using a DateTime object and one using a simple counter. You may find that one of these is better suited for your needs.

有关您可能要考虑作为用户输入的消息列表,请访问

For the list of messages you may want to consider as user input, visit here.

这篇关于如何确定C#应用程序中的空闲时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆