public MainWindow() { StateWindow sw = new StateWindow(); InitializeComponent(); Loaded += MainWindow_Loaded; Closing += MainWindow_Closing; // コンポーネントの状態を初期化 lblTime.Content = "00:00:00"; sw.lblTotalTime.Content = "00:00:00"; // タイマーのインスタンスを生成 dispatcherTimer = new DispatcherTimer(DispatcherPriority.Normal); dispatcherTimer.Interval = new TimeSpan(0, 0, 1); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimerState = new DispatcherTimer(DispatcherPriority.Normal); dispatcherTimerState.Interval = new TimeSpan(0, 0, 1); dispatcherTimerState.Tick += new EventHandler(dispatcherTimer_Tick); //グラフでつかうタイマーインスタンス glafTimer = new DispatcherTimer(DispatcherPriority.Normal); glafTimer.Tick += new EventHandler(glafTimer_Tick); glafTimer.Interval += new TimeSpan(1, 0, 0); glafTimer.Start(); oldglaftimespan = oldtimespan.Add(nowtimespan); }
//別ウィンドウの表示 private void State_open_Click(object sender, RoutedEventArgs e) { StateWindow sw = new StateWindow(); sw.lblTotalTime.Content = stateoldtimespan.Add(statenowtimespan).ToString(@"hh\:mm\:ss"); sw.Show(); }
// タイマー操作:リセット private void TimerReset() { StateWindow sw = new StateWindow(); oldtimespan = new TimeSpan(); stateoldtimespan = new TimeSpan(); lblTime.Content = "00:00:00"; sw.lblTotalTime.Content = "00:00:00"; }
// タイマー Tick処理 void dispatcherTimer_Tick(object sender, EventArgs e) { StateWindow sw = new StateWindow(); nowtimespan = DateTime.Now.Subtract(StartTime); statenowtimespan = DateTime.Now.Subtract(StateStartTime); lblTime.Content = oldtimespan.Add(nowtimespan).ToString(@"hh\:mm\:ss"); //経過を知らせてくれるけど止まるコード /*if (TimeSpan.Compare(oldtimespan.Add(nowtimespan), new TimeSpan(0, 0, TimeLimit)) >= 0) * { * MessageBox.Show(String.Format("{0}秒経過しました。", TimeLimit), * "Infomation", MessageBoxButton.OK, MessageBoxImage.Information); * }*/ if (TimeSpan.Compare(oldtimespan.Add(nowtimespan), new TimeSpan(timeLimitHH, timeLimitMM, 0)) >= 0) { MessageBox.Show(String.Format("目標作業時間に到達しました、おめでとうございます!"), "Infomation", MessageBoxButton.OK, MessageBoxImage.Information); } //メッセージボックスを出すとMainWindowが停止する、目標時間達成の知らせが出続ける }