static void Main(string[] args) { while (true) { long x = IdleTimeFinder.GetLastInputTime(); while (x == IdleTimeFinder.GetLastInputTime()) { } Console.WriteLine("You have interacted with the device"); } }
public void GetLastInputTime_Succeeds() { //arrange uint lastInputTicks; //act lastInputTicks = IdleTimeFinder.GetLastInputTime(); long lastInputMs = Environment.TickCount - lastInputTicks; TimeSpan idleTimeSpan = TimeSpan.FromMilliseconds(lastInputMs); //assert //will fail if you were using the kb/mouse actively during test //what can I say it's a kvicky Assert.IsTrue(idleTimeSpan.TotalMilliseconds > 0); }
static void Main() { // Initialize variabales int sleepTime = 10 * 60 * 1000; // 10 minutes bool displayed = false; // for warning/alert message long LastInputTime = IdleTimeFinder.GetLastInputTime(); // DateTime StartupTime = DateTime.Now; // Else would be undefined SoundPlayer alertSound = new SoundPlayer(@"C:\Users\Tze Zhao\Desktop\C#\AutoSleep\Google_event_notification_tone.wav"); //Initiate Sound player AutoClosingMessageBox.Show("AutoSleep starting", "AutoSleep.exe", 5000); // Dialog to show that program is running while (true) // infinite loop to ensure program is running despite waking up from sleep { // To display warning message if (IdleTimeFinder.GetIdleTime() > sleepTime - 60000) // Check if there is one minute left before shutdown { if (displayed) { if (IdleTimeFinder.GetLastInputTime() != LastInputTime) // means that user has interacted with device { LastInputTime = IdleTimeFinder.GetLastInputTime(); // update last interacted time displayed = false; // computer to display again the next time device is going to sleep } } else { alertSound.Play(); // play alert sound AutoClosingMessageBox.Show("Going to sleep in 1 min", "AutoSleep.exe", 30000); // alert user that computer is going to sleep displayed = true; // computer has displayed } } // To put PC to sleep //&& (StartupTime.AddMinutes(sleepTime / 60000) > DateTime.Now if (IdleTimeFinder.GetIdleTime() > sleepTime) // Check if 10 minutes have passed since last user interaction //and waking up from sleep { Application.SetSuspendState(PowerState.Suspend, true, true); // Sleep AutoClosingMessageBox.Show("AutoSleep starting", "AutoSleep.exe", 5000); // Dialog to show that program is running after waking up //StartupTime = DateTime.Now; } Thread.Sleep(1000); // (Power optimization) Makes the program wait 1 second before refreshing, prevents overconsumption of CPU power //takes about 20% of CPU if constantly checking for idle time (while loop) } }
public async Task Execute(IJobExecutionContext context) { if (InputIdleLockHelper.Instance.LockDisable) { return; } if (!InputIdleLockHelper.Instance.CheckInputIdleTime) { return; } DateTime dateTime; long ticks = IdleTimeFinder.GetLastInputTime(out dateTime); dateTime = dateTime.AddMinutes(InputIdleLockHelper.Instance.Minute); if (dateTime < DateTime.Now) { InteropHelper.LockWorkStation(); } }