2009-10-09 4 views

Répondre

12

Vous pouvez utiliser ces fonctions

voir ce code, vous devez ajouter une minuterie à votre formulaire et définissez 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

Savez-vous comment obtenir une application MVC? Ce – alice7

+3

détecte lorsque le système dans son ensemble est inactif, pas quand une application spécifique est inactif. –

0

Définir un délai d'attente de la charge, et chaque fois qu'une action « active » se produit (vous devrez brancher les), remis à zéro le minuteur de retour au début.

+0

y at-il des exemples d'applications disponibles? – Sauron

0

OMI, la réponse acceptée est pas aussi bon que cette méthode:

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

L'article CodeProject utilise des messages Windows qui provoque le composant à considérer l'application ne chôme pas, par exemple

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>