2012-03-20 60 views
0

我正在使用Windows服務,需要知道本地計算機閒置了多長時間。我已經嘗試了標準的Qt方法,但由於該服務是以LocalSystem身份運行的,因此它不會註冊本地用戶活動。系統空閒時間 - Windows服務

有關如何在應用程序以LocalSystem身份運行時獲取機器空閒狀態的任何想法?

+0

可能重複(http://stackoverflow.com/questions/6494436 /越來越多的用戶空閒時間在C) – 2012-03-20 21:35:58

+0

我會假設你會檢測是否有用戶登錄之前,確定空閒時間... – 2012-03-20 21:40:08

+0

決明子似乎只能與一個終端服務器,而不是本地機器... – user1282008 2012-03-21 16:06:37

回答

0

不知道這是否有幫助。從文章:here

由於我們使用的非託管庫,首先出現的額外using語句:?獲取用戶的空閒時間在C#]的

using System.Runtime.InteropServices; 

// Unmanaged function from user32.dll  
[DllImport("user32.dll")]  
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); 
// Struct we'll need to pass to the function  
internal struct LASTINPUTINFO  
{  
    public uint cbSize;  
    public uint dwTime;  
} 

private void tmrIdle_Tick(object sender, EventArgs e)  
{  
    // Get the system uptime  
    int systemUptime = Environment.TickCount;  
    // The tick at which the last input was recorded  
    int LastInputTicks = 0;  
    // The number of ticks that passed since last input  
    int IdleTicks = 0;    
    // Set the struct  
    LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();  
    LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo);  
    LastInputInfo.dwTime = 0;  

    // If we have a value from the function  
    if (GetLastInputInfo(ref LastInputInfo))  
    {  
     // Get the number of ticks at the point when the last activity was seen  
     LastInputTicks = (int)LastInputInfo.dwTime;  
     // Number of idle ticks = system uptime ticks - number of ticks at last input  
     IdleTicks = systemUptime - LastInputTicks;  
    }   

    // Set the labels; divide by 1000 to transform the milliseconds to seconds  
    lblSystemUptime.Text = Convert.ToString(systemUptime/1000) + " seconds";  
    lblIdleTime.Text = Convert.ToString(IdleTicks/1000) + " seconds";  
    lblLastInput.Text = "At second " + Convert.ToString(LastInputTicks/1000);  
} 
+0

我試過了,但它似乎查看LocalSystem的不活動狀態,並報告系統自啓動以來一直處於非活動狀態... – user1282008 2012-03-21 05:12:19

+0

如果您看我的第一條評論在你的問題上,你會看到它使用相同的代碼,但是當用戶登錄時啓動這個過程。絕對是一個重複的問題。 – 2012-03-21 17:17:53