How do I determine idle time in my C# application?

后端 未结 3 1313
耶瑟儿~
耶瑟儿~ 2021-01-03 10:17

I want to get the Windows Idle Time from my application. I am using exactly this code:

http://dataerror.blogspot.de/2005/02/detect-windows-idle-time.html

I h

3条回答
  •  长情又很酷
    2021-01-03 10:32

    Here is a sample code of how you can detect idle time in Windows. The definition of idle time here refer to the time when there is no user interaction with Windows such as no keyboard and mouse input.

    Detecting idle time is used in application like MSN Messenger to change the status to "Away" after the user does not interact with the Windows for a predefine time.

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace GetLastInput_Demo
    {
        /// 
        /// Summary description for Form1.
        /// 
        public class Form1 : System.Windows.Forms.Form
        {
            [DllImport("user32.dll")]
            static extern bool GetLastInputInfo(out LASTINPUTINFO plii);
    
            [StructLayout( LayoutKind.Sequential )]
            struct LASTINPUTINFO
            {
                public static readonly int SizeOf =
                       Marshal.SizeOf(typeof(LASTINPUTINFO));
    
                [MarshalAs(UnmanagedType.U4)]
                public int cbSize;   
                [MarshalAs(UnmanagedType.U4)]
                public int dwTime;
            }
    
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Timer timer1;
            private System.ComponentModel.IContainer components;
    
            public Form1()
            {
                //
                // Required for Windows Form Designer support
                //
                InitializeComponent();
    
            }
    
            /// 
            /// Clean up any resources being used.
            /// 
            protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }
            #region Windows Form Designer generated code
            private void InitializeComponent()
            {
                // Code is omitted here.
            }
            #endregion
    
            /// 
            /// The main entry point for the application.
            /// 
            [STAThread]
            static void Main()
            {
                Application.Run(new Form1());
            }
    
            private void button1_Click(object sender, System.EventArgs e)
            {
                timer1.Enabled = !timer1.Enabled;
            }
    
            private void timer1_Tick(object sender, System.EventArgs e)
            {
                int idleTime = 0;
                LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
                lastInputInfo.cbSize = Marshal.SizeOf( lastInputInfo );
                lastInputInfo.dwTime = 0;
    
                int envTicks = Environment.TickCount;
    
                if( GetLastInputInfo( out lastInputInfo ) )
                {
                    int lastInputTick = lastInputInfo.dwTime;
                    idleTime = envTicks - lastInputTick;
                }
    
                int a;
               
                if(idleTime > 0)
                   a = idleTime / 1000;
                else
                   a = idleTime;
    
                label1.Text = a.ToString();
            }
        }
    }
    

提交回复
热议问题