// play a notification sound every NOTIFYTIME seconds until signalled void IntervalNotify() { while (!NotifyBlocker.WaitOne(NOTIFYTIME)) // while not signalled { NotificationSound.Play(); } // reset signal for next notifyblock NotifyBlocker.Reset(); }
void Timer() { string periodLabel = ""; int periodTime = 0; bool isWorkPeriod = true, wasPaused = false, wasRestarted = false; DateTime currentTime, periodEndTime; while (Command != QUIT) { currentTime = DateTime.Now; if (wasPaused) { // do not reset label or time, use saved periodTime wasPaused = false; periodEndTime = currentTime.AddMilliseconds(periodTime); Console.WriteLine("[{0}] Resumed: {1} | End: {2} ({3})", periodLabel, currentTime.ToString("h:mm:ss tt"), periodEndTime.ToString("h:mm:ss tt"), (periodEndTime - currentTime).ToString(@"mm\:ss")); } else if (wasRestarted) { // do not reset label or time, use existing or modified periodTime wasRestarted = false; periodEndTime = currentTime.AddMilliseconds(periodTime); Console.WriteLine("[{0}] Restarted: {1} | End: {2} ({3})", periodLabel, currentTime.ToString("h:mm:ss tt"), periodEndTime.ToString("h:mm:ss tt"), (periodEndTime - currentTime).ToString(@"mm\:ss")); } else // normal flow { if (isWorkPeriod) { periodLabel = "WORK"; periodTime = WORKTIME; } else { periodLabel = "REST"; periodTime = RESTTIME; } periodEndTime = currentTime.AddMilliseconds(periodTime); Console.WriteLine("[{0}] Start: {1} | End: {2} ({3})", periodLabel, currentTime.ToString("h:mm:ss tt"), periodEndTime.ToString("h:mm:ss tt"), (periodEndTime - currentTime).ToString(@"mm\:ss")); } // block thread for periodTime if (TimerBlocker.WaitOne(periodTime)) // if signalled { // manually signalled, reset signal for next timerblock TimerBlocker.Reset(); // handle commands if (Command == PAUSE) { TimeSpan remainingPeriod = periodEndTime - DateTime.Now; periodTime = (int)remainingPeriod.TotalMilliseconds; Console.Write("Period paused, press enter to resume "); WaitForReadLineInput(); wasPaused = true; } else if (Command == NEXT) { isWorkPeriod = !isWorkPeriod; } else if (Command.Contains(RESTART)) { wasRestarted = true; // checking if "MM:SS" setting option present MatchCollection numsInCommand = new Regex(@"\d+").Matches(Command); if (numsInCommand.Count >= 2) { int minsTemp = Int32.Parse(numsInCommand[0].Value); int secsTemp = Int32.Parse(numsInCommand[1].Value); // converting to milliseconds minsTemp *= 60000; secsTemp *= 1000; periodTime = minsTemp + secsTemp; } else { // else restart with normal period time periodTime = isWorkPeriod ? WORKTIME : RESTTIME; } } // else Command == "quit" -> do nothing, while condition will handle it } else // if not signalled (timeout) { isWorkPeriod = !isWorkPeriod; PeriodEndSound.Play(); Thread notifyThread = new Thread(IntervalNotify); notifyThread.Start(); Console.WriteLine("Period end, press Insert to resume"); // blocks until Insert is pressed InterceptKeys.Start(KeyPressCallback); // end notifyThread NotifyBlocker.Set(); // play a confirmation NotificationSound.Play(); } } }