Log idle time

Discussion in 'C#' started by meniscus, Dec 19, 2007.

  1. meniscus

    meniscus New Member

    Joined:
    Dec 19, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi everyone,

    Im trying to carry out a simple task (or so i thought!) of logging my PCs idle time (allowing a grace period of 20mins) to a text file residing on my desktop. Im guessing an if statement will check the 20 minutes interval for me!) The problem im having is accessing the idle time.

    Im using the following code but getting nowhere. Does anyone have any pointers as to what im doing wrong?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices; 
    
    
    
    
      public class Idle 
      { 
    
        [DllImport("User32.dll")] 
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO lastInput);
    
    
        [StructLayout(LayoutKind.Sequential)] //Indicates that the members of the type are to be laid out in unmanaged memory in the same order in which they appear in the managed type definition
          public struct LASTINPUTINFO 
        { 
          public Int32 cbSize; 
          public Int32 dwTime; 
        } 
    
    
        public int IdleTime 
        { 
          get 
          { 
    
            LASTINPUTINFO lastInput = new LASTINPUTINFO(); 
            lastInput.cbSize = Marshal.SizeOf(lastInput); 
    
            int idle = 0;
    
            if (GetLastInputInfo(ref lastInput)) 
              idle = (Environment.TickCount - lastInput.dwTime) / 1000;
    
            return idle; 
          } 
        }
         
          
          
          class MainProgram
        {
            public static void Main()
            {
    
                Idle idlemins = new Idle();
                Console.WriteLine("Idletime is", idlemins.IdleTime);
                Console.ReadLine();
            }
        }
    
          
    
      }

    P.S Im very much new to programming (Only finished day 7 in Sams teach yourself C# in 21 days)

    Thanks for your patience,
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice