2009-10-09 7 views

risposta

12

È possibile utilizzare queste funzioni

vedono questo codice, è necessario aggiungere un timer al form e impostare 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

Sai come ottenere per l'applicazione MVC? – alice7

+3

Rileva quando il sistema nel suo complesso è inattivo, non quando un'applicazione specifica è inattiva. –

0

Impostare un timeout sul carico e ogni volta che si verifica un'azione "attiva" (è necessario collegarsi ad essi), reimpostare il timer fino all'inizio.

+0

Sono disponibili applicazioni di esempio? – Sauron

0

IMO la risposta accettata, non è buono come questo metodo:

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

L'articolo CodeProject utilizza i messaggi di Windows che farà sì che la componente di prendere in considerazione l'applicazione non inattivo, ad esempio

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> 
Problemi correlati