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 f开发者_运维技巧orm app.
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.
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.
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
DialogResult result = MessageBox.Show("There was an error, would you like to continue?", "Error",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.No)
{
// Terminate
}
精彩评论