// 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(); }
private void ClientSocket_OnMessagereceived(SocketMessage e) { string dataString = e.GetValue("DataType") == null ? "" : e.GetValue("DataType").ToString(); Reply.ContentType dataType = (Reply.ContentType)Enum.Parse(typeof(Reply.ContentType), dataString); string[] urls = null; if (e.Data.ContainsKey("Links")) { urls = JsonConvert.DeserializeObject <string[]>(JsonConvert.SerializeObject(e.Data["Links"])); } MessageBlock block; if (dataType == Reply.ContentType.Picture && urls != null) { block = new MessageBlock(urls, e.GetValue("Message").ToString()); } else { block = new MessageBlock(); } string timeStamp = e.GetValue("TimeStamp").ToString(); timeStamp = DateTime.Parse(timeStamp).ToString("h:mm tt"); block.Message = e.GetValue("Message").ToString(); block.Sender = e.GetValue("Sender").ToString(); block.Date = timeStamp; block.MaximumSize = new Size(((InnerChatContainer.Width - 20) / 2) - 20, int.MaxValue); Invoke(new MethodInvoker(() => { block.FormatSize(); InnerChatContainer.Controls.Add(block); SelectedChat.LastMessage = e.GetValue("Message").ToString(); SelectedChat.TimeStamp = timeStamp; })); if ((DateTime.Now - lastNotification).TotalSeconds > 5 && block.Sender != CurrentUser.UserName && !Activated) { lastNotification = DateTime.Now; NotificationSound.Play(); } ResizeChat(); }
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(); } } }