2009-10-09 10 views

Odpowiedz

12

Można korzystać z tych funkcji

zobaczyć ten kod, należy dodać czasomierza do formularza i ustawić 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

Czy wiesz, jak osiągnąć dla aplikacji MVC? – alice7

+3

Umożliwia wykrycie, kiedy system jako całość jest bezczynny, a nie gdy określona aplikacja jest bezczynna. –

0

Ustaw limit czasu na obciążenie i za każdym razem, gdy wydarzy się "aktywna" akcja (musisz się do nich podłączyć), zresetuj licznik czasu z powrotem na początek.

+0

Czy są dostępne przykładowe aplikacje? – Sauron

0

IMO przyjął odpowiedź nie jest tak dobra jak ta metoda:

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

Artykuł CodeProject wykorzystuje wiadomości Windows, które spowodują komponent rozważyć zastosowanie nie idle, np

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> 
Powiązane problemy