private void UpdateProgress(TimeSpan elapsed, TimeSpan total) { var newProgress = Math.Min(100.0, 100.0 * elapsed.Ticks / total.Ticks); if (MainProgressBar.Value != newProgress) { MainProgressBar.Value = newProgress; TaskbarUtility.SetProgressValue(interopHelper.Handle, (ulong)MainProgressBar.Value, 100); } }
private void UpdateInterface() { while (true) { DispatcherOperation op = Dispatcher.BeginInvoke(new Action(delegate() { DateTime now = DateTime.Now; TimeSpan elapsed = now - start; TimeSpan remaining = end - now; TimeSpan total = end - start; if (remaining.Ticks <= 0) { if (!MainTextBox.IsFocused) { MainTextBox.Text = "Timer expired"; } if (notifyIcon != null) { notifyIcon.Text = "Timer expired"; } Title = "Orzeszek Timer"; MainProgressBar.Value = 100; TaskbarUtility.SetProgressState(interopHelper.Handle, TaskbarProgressState.NoProgress); if (!notified) { try { notified = true; string exePath = Assembly.GetExecutingAssembly().Location; string exeDir = new FileInfo(exePath).DirectoryName; string soundsDir = System.IO.Path.Combine(exeDir, "Sounds"); SoundPlayer sp = new SoundPlayer(System.IO.Path.Combine(soundsDir, Settings.Default.AlarmSound)); if (Settings.Default.CloseOnFinish || closeOnFinishThisTime) { sp.PlaySync(); } else if (Settings.Default.LoopNotification) { if (notification != null) { notification.Stop(); } notification = sp; notification.PlayLooping(); StopNotificationButton.Visibility = System.Windows.Visibility.Visible; } else { sp.Play(); } if (Settings.Default.PopupOnFinish) { Show(); WindowState = restorableWindowState; Activate(); Topmost = true; Topmost = false; } if (Settings.Default.FlashOnFinish) { TaskbarUtility.StartFlash(interopHelper.Handle); } } catch (Exception) { } } if (Settings.Default.CloseOnFinish || closeOnFinishThisTime) { closeFromTray = true; Close(); } else if (Settings.Default.LoopTimer && Settings.Default.LastTimeSpan != TimeSpan.Zero) { start = DateTime.Now; end = start.Add(Settings.Default.LastTimeSpan); notified = false; } else { updaterThread.Abort(); updaterThread = null; } } else { if (!MainTextBox.IsFocused) { MainTextBox.Text = ToString(remaining); } if (notifyIcon != null) { notifyIcon.Text = ToString(remaining); } Title = MainTextBox.Text; MainProgressBar.Value = Math.Min(100.0, 100.0 * elapsed.Ticks / total.Ticks); TaskbarUtility.SetProgressValue(interopHelper.Handle, (ulong)MainProgressBar.Value, 100); } } )); op.Wait(); Thread.Sleep((int)Math.Max(Math.Min((end - start).TotalSeconds / MainProgressBar.ActualWidth, 1000), 10)); } }