tags:

views:

57

answers:

5

I have a winform application that can catch any number of possible errors. I have found though, that once a messagebox or some other method of displaying the error has been displayed (within a catch block), the program execution continues.

How can I simply stop complete execution of the program in this event and simply just keep the form open? It is only a one form app.

+1  A: 

After the message box is displayed, simple call Application.Exit();

This will suffice as long as you don't have any other running threads in the background, but in your case it seems that this is just a simple single threaded application.

Øyvind Bråthen
I think with stop he means suspend/pause and not terminate.
CodeInChaos
This doesn't work? It still shows the messagebox, but then the application continues? Any ideas why? It is just a single threaded app.
darren young
Are all this happening inside the constructor? Are the try/catch, messagebox and handling inside the constructor of the Form?
Øyvind Bråthen
If the application continues, you're not calling `Application.Exit`...
Dan Puzey
Actually, if you call Application.Exit inside the constructor of the form you open using Application.Run to start, then the program will not stop, since it's not until the constructor is finished that Application.Run will actually run the form, so the Exit occurs before Run. Otherwise, it should close under other circumstances.
Øyvind Bråthen
No, i's not within the constructor, it's just a method that is called from somewhere else. The method searches for specific sheets within and Excel spreadsheet and if it cannot find them, then it would throw an exception (which it does), when I catch that exception, I want it to halt the process as any attempt to go further would not work as the specified sheets are not available. Thanks
darren young
A: 

You might want to set the owner window of the modal dialog to your form. That way the execution isn't suspended, but the form is deactivated.

CodeInChaos
A: 

Kill the application with Application.Exit(); and just before that launch a new process which will display the MessageBox to the user.

Daniel Mošmondor
A new process just to display a MessageBox? Really?
Dan Puzey
I've tried that. I basically have: [code] try { sheet = (Excel.Worksheet)sheets.get_Item(sht); copyToClipboard(sheet); } catch (Exception) { error(); Application.Exit(); }[/code] However, this is not working?
darren young
Dan: what is the cost of a new process?
Daniel Mošmondor
A: 

Presuming you have something like this:

Private Sub Button1_OnCLick(....) handles button1.onclick
  If somecondition then
    MsgBox("it failed")
  End if
  'more code here

and you want to avoid executing 'more code' when the message box has been shown, then add

Exit Sub

just after the MsgBox line

smirkingman
A: 
DialogResult result = MessageBox.Show("There was an error, would you like to continue?", "Error",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result == DialogResult.No)
{
   // Terminate
}
kyndigs