4

I'm having a bit weird problem with WinForm which seems to refuse to close for some weird reason. I've got very simple gui which sometimes doesn't react for me pressing X or when i use events on buttons it even reaches Close() and does nothing..

    private void buttonZapisz_Click(object sender, EventArgs e) {
        string plik = textBoxDokumentDoZaladowania.Text;
        if (File.Exists(plik)) {
            string extension = Path.GetExtension(plik);
            string nazwaPliku = Path.GetFileName(plik);

            SqlMethods.databaseFilePut(plik, comboBoxTypDokumentu.Text, textBoxKomentarz.Text, sKlienciID, sPortfelID, extension, nazwaPliku);
            Close();
        }
    }

There are no events assigned to FormClosed or FormClosing. So how can I find out what's wrong. Sometimes X will work after the GUI is loaded but after i press Button to save some stuff to database it reaches Close() in that button event and it still is visible and does nothing. Can't use X, nor ALT+F4. I can go around GUI and choose other values for ComboBox without problem.

I call GUI like this:

    private void contextMenuDokumentyDodaj_Click(object sender, EventArgs e) {
        var lv = (ListView) contextMenuDokumenty.SourceControl;
        string varPortfelID = Locale.ustalDaneListViewKolumny(listViewNumeryUmow, 0);
        string varKlienciID = Locale.ustalDaneListViewKolumny(listViewKlienci, 0);

        if (lv == listViewDokumentyPerKlient) {
            if (varKlienciID != "") {
                var dokumenty = new DocumentsGui(varKlienciID);
                dokumenty.Show();
                dokumenty.FormClosed += varDocumentsGuiKlienci_FormClosed;
            }
        } else if (lv == listViewDokumentyPerPortfel) {
            if (varPortfelID != "" && varKlienciID != "") {
                var dokumenty = new DocumentsGui(varKlienciID, varPortfelID);
                dokumenty.Show();
                dokumenty.FormClosed += varDocumentsGuiPortfele_FormClosed;
            }
        } 
    }

While I can't close GUI i can work on the main gui without problem too. I can open up same GUI and after opening new GUI i can quickly close it. GUI is very simple with few ComboBoxes,TextBoxes and one EditButton from Devexpress.

Edit: varDocumentsGuiPortfele_FormClosed code allows me to refresh GUI (reload ListView's depending on where the user is on now).

    private void varDocumentsGuiPortfele_FormClosed(object sender, FormClosedEventArgs e) {
        TabControl varTabControl = tabControlKlientPortfele;

      if (varTabControl.TabPages.IndexOf(tabPageDokumentyPerKlient) == varTabControl.SelectedIndex) {
          loadTabControlKlientPortfeleBezZmianyUmowy();

      }
    }
MadBoy
  • 10,824
  • 24
  • 95
  • 156
  • Do you have any `FormClosing` handlers? – SLaks Nov 13 '11 at 17:41
  • Nope. Nothing. GUI is very simple and I've not done much of setting things up for it. – MadBoy Nov 13 '11 at 17:42
  • 1
    You say "*there are no events assigned to `FormClosed` or `FormClosing`*" but in your code you have `dokumenty.FormClosed += varDocumentsGuiKlienci_FormClosed;`. What's in `varDocumentsGuiKlienci_FormClosed`? – Otiel Nov 13 '11 at 17:44
  • Do you have any background threads running? – 3Dave Nov 13 '11 at 17:48
  • Otiel this _FormClosed is in MainForm and I use it to refresh ListView's in main gui when user closes the Save Form. I've been using it like that for ages with no problems.. but after I've commented out FormClosed in the MainForm gui other gui closes now without problems... I've edited main post with the code for Form_Closed. – MadBoy Nov 13 '11 at 17:49
  • @DavidLively the FormClosed does have method inside that spawns new "quick" thread to check for database size and update MainForm. `subThreadForStatistics = new Thread(sprawdzWielkoscBazy); subThreadForStatistics.Start();` – MadBoy Nov 13 '11 at 17:51
  • @Madboy try disabling that and see what happens. Threads that don't exit can cause issues when the app is trying to close. – 3Dave Nov 13 '11 at 18:28
  • @David Lively it was CLR Exceptions that were turned off for System.Data (not sure why) and I wasn't getting exceptions thrown so the GUI was just doing nothing on error. After using Hans advice it works like a charm ;) – MadBoy Nov 13 '11 at 18:34

2 Answers2

16

Paste this code into your form classes:

    protected override void OnFormClosing(FormClosingEventArgs e) {
        e.Cancel = false;
        base.OnFormClosing(e);
    }

When that works, you want to find out why you have Validating event handlers that don't want the form to be closed.

Next thing you want to verify is Debug + Exceptions, tick the Thrown box for CLR Exceptions. This makes sure you don't swallow an exception that prevents a form from closing. Or worse, the operating system swallowing the exception, a nasty Windows 7 problem.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Uncommenting Form_closed in maingui and adding this one into Form's code makes no difference. GUI won't close. After I comment out FormClosed method in MainForm it will close GUI without problem. – MadBoy Nov 13 '11 at 17:55
  • Post updated. Look in the Output window too for "first chance" exception notifications. – Hans Passant Nov 13 '11 at 17:56
  • I was getting `First chance` exception when I was pressing EditButton but I followed http://stackoverflow.com/questions/204178/first-chance-exception-at-addr-in-myapp-0x000006ba-the-rpc-server-is-unava and went ahead and disabled some Exceptions as basically it was pointing me to assembled code – MadBoy Nov 13 '11 at 18:00
  • It seems some CLR Exceptions were unchecked (not sure why, I didn't touch them). After checking them it found me some errors in some method. After that everything seems to be fine now. Thanks. – MadBoy Nov 13 '11 at 18:15
  • This is pretty unhealthy. Remove try/catch blocks in your code and solve real problems instead of hiding them. – Hans Passant Nov 13 '11 at 18:23
  • I wasn't having try/catch blocks. I just had some CLR Exceptions unchecked (not sure why - including System.Data). I don't use try/catch much. I unchecked exceptions in Win32 Exceptions / Native Run-Time Checks as the `first chance` exception was getting me some weird assembly that had no meaning for me.. But after I went to CLR Exceptions and enabled all as per your advise it showed me where the problems were in my code so I've got it up and running now :-) – MadBoy Nov 13 '11 at 18:31
1

If you are getting an Exception in your close method, then the Base closing method is never called.

Put a try{}catch{} around everything

penguin
  • 11
  • 1
  • This helped me notice the exception in my Debug output window, and remove the exception cause, so thank you for that. – Slagh May 05 '17 at 10:39