2012-11-03 56 views
0

我想從我的應用程序獲得Windows空閒時間。我使用的正是這種代碼:如何確定我的C#應用​​程序中的空閒時間?

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

我已經在Windows 7測試這一點,它工作正常,但我只獲得在Windows 8中的零

任何想法如何解決這一問題?

+0

「正常工作」是否意味着您在Windows 7中獲得了正確的數據? – jheddings

+0

是的。 我也可以在這裏分享Exec的,但我認爲有些人會認爲它是一個病毒等 – Styler2go

+0

它的工作原理我的電腦(在Win8)上! –

回答

6

我參加了一個稍微不同的方法,以你的方式......這就決定了空閒時間,你的應用程序,而不是使用系統範圍內的空閒時間。我不確定這是否符合您的需求,但它可能會幫助您進一步獲得。它還具有純.NET的優點,而不是使用DllImport

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對象,並使用一個簡單的計數器一個兩種方法。您可能會發現其中一種更適合您的需求。

對於您可能要考慮爲用戶輸入的信息,請訪問here消息列表。

+1

但我需要真正的afk時間給用戶。系統範圍。 – Styler2go

+1

對我來說真的很有幫助的代碼。 – SolessChong

1
protected override void OnLoad(EventArgs e) 
    { 
    /* Check if we are in idle mode - No mouse movement for 2 minutes */ 
    System.Windows.Forms.Timer CheckIdleTimer = new     System.Windows.Forms.Timer(); 
    CheckIdleTimer.Interval = 120000; 
    CheckIdleTimer.Tick += new EventHandler(CheckIdleTimer_Tick); 
    CheckIdleTimer.Start(); 
    } 
    private void CheckIdleTimer_Tick(object sender, EventArgs e) 
    { 
    uint x = IdleTimeFinder.GetIdleTime(); 
    if (x > 120000) { 
     //...do something 
    } 
    } 


    public class IdleTimeFinder 
    { 
    [DllImport("User32.dll")] 
    private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); 

    [DllImport("Kernel32.dll")] 
    private static extern uint GetLastError(); 

    public static uint GetIdleTime() 
    { 
    LASTINPUTINFO lastInPut = new LASTINPUTINFO(); 
    lastInPut.cbSize  (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut); 
    GetLastInputInfo(ref lastInPut); 
    return ((uint)Environment.TickCount - lastInPut.dwTime); 
    } 

    public static long GetLastInputTime() 
    { 
    LASTINPUTINFO lastInPut = new LASTINPUTINFO(); 
    lastInPut.cbSize    (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut); 
    if (!GetLastInputInfo(ref lastInPut)) 
    { 
    throw new Exception(GetLastError().ToString()); 
    } 
    return lastInPut.dwTime; 
    } 
    } 


internal struct LASTINPUTINFO 
{ 
    public uint cbSize; 

    public uint dwTime; 
} 
相關問題