// イベントハンドラー private async void toggleButton_Click(object sender, RibbonControlEventArgs e) { try { if (this.Task == null || this.Task.IsCompleted) { var lineArt = this.CreateLineArt(); try { this.apexCountDropDown.Enabled = false; this.afterImageCountDropDown.Enabled = false; int apexCount = (int)this.apexCountDropDown.SelectedItem.Tag; int afterImageCount = (int)this.afterImageCountDropDown.SelectedItem.Tag; this.Task = new CancelableTask(canceler => lineArt.Start(apexCount, afterImageCount, canceler)); if (background) { this.Task.Start(); await this.Task; } else { this.Task.RunSynchronously(); } } finally { this.Task?.Dispose(); this.Task = null; if (!this.ApplicationClosed) { this.apexCountDropDown.Enabled = true; this.afterImageCountDropDown.Enabled = true; this.toggleButton.Enabled = true; this.toggleButton.Checked = false; } } } else { this.Task.Cancel(); this.toggleButton.Enabled = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } }
private async void StartButton_Click(object sender, RoutedEventArgs e) { int apexCount = this.GetApexCount(); int afterImageCount = this.GetAfterImageCount(); var lineArt = new LineArt(this, this.canvas); using (this.task = new CancelableTask(canceler => lineArt.Start(apexCount, afterImageCount, canceler))) { this.startButton.IsEnabled = false; this.stopButton.IsEnabled = true; this.apexCountComboBox.IsEnabled = false; this.afterImageCountComboBox.IsEnabled = false; this.task.Start(); await this.task; this.canvas.Children.OfType <System.Windows.Shapes.Line>().ToList().ForEach(this.canvas.Children.Remove); this.startButton.IsEnabled = true; this.stopButton.IsEnabled = false; this.apexCountComboBox.IsEnabled = true; this.afterImageCountComboBox.IsEnabled = true; } }