More races all the good time of day There was such a problem (WPF). There is a click handler, it is executed, that is, it launches the file for installation, But if the user cancels the installation, the program hangs and throws an exception that the user canceled the action. What could be the matter please tell me.

private void InstallLBSelectCh(object sender, SelectionChangedEventArgs e) { System.Diagnostics.Process p = new System.Diagnostics.Process(); switch (InstallLB.SelectedIndex) { case 0: p.StartInfo.FileName = GetFiles[InstallLB.SelectedIndex]; p.Start(); break; case 1: p.StartInfo.FileName = GetFiles[InstallLB.SelectedIndex]; p.Start(); break; case 2: p.StartInfo.FileName = GetFiles[InstallLB.SelectedIndex]; p.Start(); break; default: goto case 0; } } 

So far I have found only such a solution.

 private void InstallLBSelectCh(object sender, SelectionChangedEventArgs e) { Process p = new Process(); switch (InstallLB.SelectedIndex) { default: case 0: p.StartInfo.FileName = GetFiles[InstallLB.SelectedIndex]; try { p.Start(); } catch { MessageBox.Show("Canceled by User"); } break; case 1: p.StartInfo.FileName = GetFiles[InstallLB.SelectedIndex]; p.Start(); break; case 2: p.StartInfo.FileName = GetFiles[InstallLB.SelectedIndex]; p.Start(); break; } } 
  • If you have cancellation through CancellationToken, then there is nothing wrong with that, just handle the error through try / catch. But goto is bad ... Better combine case ( default: case0: , than throw over goto! - EvgeniyZ
  • The fact of the matter is that I don’t have a cancellation, an exception pops up on it, and I can’t think how to realize that the user canceled the installation of the file and that the program would not freeze but continue to work, but output a message to the user that he canceled this action. - mindevis

0