2009-10-09 52 views

回答

12

您可以使用这些功能

看到这个代码,您必须将计时器添加到您的形式,并设置this.timer1.Enabled = TRUE;

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace WindowsFormsApplication9 
{ 
    internal struct LASTINPUTINFO 
    { 
    public uint cbSize;  
    public uint dwTime; 
    } 

    public partial class Form1 : Form 
    { 

    [DllImport("User32.dll")] 
    public static extern bool LockWorkStation(); 
    [DllImport("User32.dll")] 
    private static extern bool GetLastInputInfo(ref LASTINPUTINFO Dummy); 
    [DllImport("Kernel32.dll")] 
    private static extern uint GetLastError(); 

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

public static long GetTickCount() 
{ 
    return Environment.TickCount; 
} 

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

    return LastUserAction.dwTime; 
} 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     if (GetIdleTime() > 10000) //10 secs, Time to wait before locking 
     LockWorkStation(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     timer1.Start(); 
    } 
    } 
} 
+0

你知道如何实现MVC应用程序? – alice7 2012-06-19 21:16:42

+3

此检测何时系统作为一个整体是空闲的,而不是当一个特定的应用程序处于空闲状态。 – 2015-06-04 16:32:17

0

设置每一个“主动”动作发生的时间(你需要连接到他们)负载超时,并且,复位定时器回到开始。

+0

是否有任何可用的示例应用程序? – Sauron 2009-10-09 06:05:49

0

IMO接受的答案是不是因为这个方法好:

http://www.codeproject.com/Articles/30345/Application-Idle

CodeProject上的文章使用Windows消息,这将导致组件考虑该申请没有闲着,如

public enum ActivityMessages : int 
{ 
    /// <summary> 
    /// Cursor moved while within the nonclient area. 
    /// </summary> 
    WM_NCMOUSEMOVE = 0x00A0, 
    /// <summary> 
    /// Mouse left button pressed while the cursor was within the nonclient area. 
    /// </summary> 
    WM_NCLBUTTONDOWN = 0x00A1, 
    /// <summary> 
    /// Mouse left button released while the cursor was within the nonclient area. 
    /// </summary> 
    WM_NCLBUTTONUP = 0x00A2, 
    /// <summary> 
相关问题