darren young
darren young

Reputation: 95

c# - Stop program execution incase of error

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.

Upvotes: 0

Views: 5322

Answers (4)

Stephen
Stephen

Reputation: 3084

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

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

Upvotes: 0

smirkingman
smirkingman

Reputation: 6358

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

Upvotes: 0

CodesInChaos
CodesInChaos

Reputation: 108790

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.

Upvotes: 0

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

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.

Upvotes: 1

Related Questions