0

My Winforms app has a dialog that opens on first run to capture a SQL connection string. The dialog is triggered by a MessageBox and the capture form shows when the user clicks "OK". What I'd like is the application to exit when the Cancel button is clicked however it's not working the way I'd expect.

while (GlobalSettings.getSqlConnection() == "")
{
    var result = MessageBox.Show("A connection to the database is required in order for the application to function!\r\nPlease enter valid connection string(s) in the next window", "Error", MessageBoxButtons.OKCancel);
    if (result.ToString() == "OK")
    {
        using (ConnectionStrings box = new ConnectionStrings())
        {
            box.ShowDialog(this);
        }
    }
    else
    {
        Application.Exit();
    }
}
// more code

I expect the application to close if the Cancel button is clicked however the "While" loop continues and the MessageBox shows again.

I realise I can create an "if" statement after this "While" loop and close from there but I'd prefer to handle it from the dialog result.

Daniel
  • 2,167
  • 5
  • 23
  • 44
  • 2
    Why not put a `break` immediately after `Application.Exit()`? – Matthew Watson Jun 14 '18 at 10:35
  • "I expect the application to close" - but it's inside a valid `while` loop. All that `Application.Exit()` does is shut down Windows Forms - it doesn't magically break the `while` loop. – Enigmativity Jun 14 '18 at 10:38
  • @MatthewWatson I tried that but this is a dialog before the main form displays & so when I insert the `break`, it breaks out of the loop but then continues with the application and displays the main form. – Daniel Jun 14 '18 at 10:39
  • Thanks for the constructive feedback @Enigmativity you're a great help – Daniel Jun 14 '18 at 10:40
  • More context - https://stackoverflow.com/a/1057159/43846 – stuartd Jun 14 '18 at 10:40
  • @Daniel - I'm glad I could provide some snarky feedback. ;-) – Enigmativity Jun 14 '18 at 10:40
  • @stuartd perhaps my approach is wrong but the `break` simply breaks out of the loop and doesn't close the application, which is what this: https://msdn.microsoft.com/en-us/library/ms157894(v=vs.110).aspx document suggests will happen – Daniel Jun 14 '18 at 10:41
  • From that link I posted above _"Application.Exit really just asks the message loop very gently. If you want your app to exit, the best way is to gracefully make it out of Main, and cleanly close any additional non-background threads. If you want to be brutal... Environment.Exit or Environment.FailFast? note this is harsh - about the same as killing your own Process."_ - are you saying if you add a `break` after `Application.Exit` then your app doesn't close? – stuartd Jun 14 '18 at 10:42
  • 2
    slightly off topic, but is there a reason your doing result.ToSTring() == "OK" vs just using result == DialogResult.OK? – Hack Jun 14 '18 at 10:42
  • @Hack No, I'm self-taught (stackoverflow taught) on winforms and this is clearly a bad habit - probably from an answer that I saw somewhere on the site – Daniel Jun 14 '18 at 10:44
  • @stuartd Yes, that's correct, the application continues to run. "The Exit method stops all running message loops on all threads and closes all windows of the application" weirdly, it's followed up by "This method does not necessarily force the application to exit" talk about a misnomer! @cy-c suggested using `Environment.Exit(0)` which I've tried and works as expected without needing the `break` – Daniel Jun 14 '18 at 10:46
  • @Daniel ah. I think everyone's been guilty of that at some point :). I see you have your answer below. I'd recommend changing the if as well. – Hack Jun 14 '18 at 10:46
  • @Hack noted! Thanks, I'll be checking up on all my dialogs to use your suggestion. – Daniel Jun 14 '18 at 10:47

1 Answers1

1

Use Environment.Exit(0) instead of Application.Exit

https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx

cy-c
  • 855
  • 8
  • 8
  • Perfect! Thanks for the suggestion, I'd come across this but the documentation here: https://msdn.microsoft.com/en-us/library/ms157894(v=vs.110).aspx lead me to believe I was doing something wrong! – Daniel Jun 14 '18 at 10:42