6

We have an application running at a client that is displaying strange behavior. At random times during the day the OK button (which calls the Form.Close method) on certain forms will not cause the Form to close. With that I mean that the user will click the button, in the trace it will display that Form.Close was called but the form will not close.

The strange thing is that the Form itself is still responsive so they can click any button on the form and the code behind the buttons will execute but the form just will not close.

This state lasts for anything between a couple of seconds to a couple of minutes. Then all of a sudden the form will start disposing and disappear. Really strange.

So to recap, the following happens:

  • Form.ShowDialog() -> The form displays
  • User works on form and presses buttons and so forth
  • Form.Close() is called -> The user clicked the close button
  • An amount of time passes where the form is just waiting to close while still being responsive (In this time the frustrated user hammered the close button a couple of times with no response)
  • All of a sudden the form disposes and a dialog result is returned from the Form.ShowDialog

Things to note:

  • I'm not using any type of threading.
  • At certain times it is reported that the explorer.exe process has stopped on the PC, can this have an impact on a form's behavior? We are scheduling a rebuild of the PC.

My question is if anyone is aware of a scenario that can cause the described behavior above?

I'm not an expert at Forms but as I understand it, when you call Form.Close, the form doesn't close instantly, the current method that called Close first finishes and then another process triggers the form to start closing and dispose.

Can this be related to the explorer.exe process not running?

Any insight would be greatly appreciated.

*** Edit

Also note that we can't replicate the issue, it happens randomly at the client.

user2418900
  • 69
  • 1
  • 5
  • 3
    Could [this](http://stackoverflow.com/questions/8113334/winforms-form-wont-close-on-pressing-x-or-close-in-c-sharp) be something for you maybe? – Simon Karlsson Jan 11 '16 at 08:37
  • https://stackoverflow.com/questions/3912836/c-sharp-why-does-form-close-not-close-the-form#answer-19313930 – online Thomas Jan 11 '16 at 08:40
  • 1
    @SimonKarlsson I had a look at your link but it's not the same scenario. The Close method is called on the modal Form being displayed itself so the issue is not with a blocking call. Also note that we can't replicate the issue, it happens randomly. – user2418900 Jan 11 '16 at 08:43
  • 1
    Without a repro, I doubt anyone will be able to help you solve it. So I'd suggest channelling you energies into creating the repro. Identify the differences between machines where it can happen and machines where it never seems to happen. Then, try to determine which of those differences are important. E.g. OS versions, other software also running, 32-bit vs 64-bit, but plenty more. – Damien_The_Unbeliever Jan 11 '16 at 08:50
  • @Damien_The_Unbeliever I suspect you are right. I've got a feeling that it's related to an issue on the environment itself (like the exporer.exe process crashing) that causes this problem. I was hoping someone else ran into this problem and can confirm that's an environmental problem. – user2418900 Jan 11 '16 at 08:55
  • Are you using Application.DoEvents somewhere? – JeffRSon Jan 11 '16 at 09:03
  • @JeffRSon I've searched the code and "Application.DoEvents" does not come up. – user2418900 Jan 11 '16 at 09:07
  • You're not using "threading" - any kind of async IO? Database access? file/network access? something similar? what could it relate to explorer - some open dialog/shell extension? – JeffRSon Jan 11 '16 at 09:12
  • @JeffRSon There's no async processing happening. I've looked at the sequence of events when calling form.close. Because the current method first finishes before the form actually closes, I assume the form.close method just marks the form in the background with a closed flag. There must be another process that initiates the actual closing and disposing of the form. Maybe because the explorer.exe process is not running or whatever caused it to crash is somehow stopping or delaying whatever triggers the form to close and dispose. I'm grasping at straws at this stage. – user2418900 Jan 11 '16 at 09:25
  • AFAICT, this.Close sends a WM_CLOSE message, which cannot be processed as long as the UI thread is active (while executing the OK button click event). Something is delaying or blocking your UI thread which seems strange because your UI is responsive. Are you receiving lots of window messages? Try Spy++ to check. – JeffRSon Jan 11 '16 at 10:22
  • Restart Explorer.exe pragmatically. [Check](http://stackoverflow.com/questions/4279293/starting-explorer-exe-isnt-working-properly-in-c-sharp/4279359#4279359) – Kashif Faraz Aug 04 '16 at 07:36

3 Answers3

1

Reminds me of C# Why does form.Close() not close the form?

Do you have any code in the button click event handler just after the this.Close() line?

What I mean:

private void OKButton_Click(object sender, EventArgs e)
{
    this.Close();

    somecodehere <-- is there any code here? (after this.Close)
}

Because Form.Close doesn't close the Form immediately, so if you have code after the call it will still execute, and maybe that's what prevents the form from closing.

zdimension
  • 977
  • 1
  • 12
  • 33
  • Thanks for your suggestion. There are some code executing after this.Close but we've put in trace logging to confirm the the method completes and exits. Also, if there were some code still executing after this.Close, the form would not have remained responsive to the users input. That is what's stumping me, that the form remains responsive but it's just not closing. – user2418900 Jan 11 '16 at 08:48
1

May be you can hide the form first before you close so that user would not get restless and try to close it

Form.Hide();
Form.Close();

Another reason is on window close or destructor may be you are trying to save a bigger file or some activity. If so try to do that in thread. So that it would close the form soon.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • Thanks for your suggestion. We thought about hiding the form but that would just be sweeping the problem under the rug and not addressing it. It could also cause memory issues since there can be multiple forms hiding in the background so we would rather make sure the form is disposed before continuing. Further, if a process was busy in the background the form would not still be responsive. – user2418900 Jan 11 '16 at 08:59
0

I agree with @zdimension answer, but I add some extra information:

  • First of all, run the application on another system, maybe you have something strange happening on this one
  • Check what you're doing after the Close() call, maybe something is slowing down the closing process
  • Check if you're doing anything in FormClosing event handler or in similar events
  • Check if you added some other callback to Click event for the close button

These things may help you find if something is going wrong, but I don't think that the problem is something you do after the Close() call, because you say that the form is still responsive.

LucaMus
  • 721
  • 7
  • 17