Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form not closing after calling Form.Close

Tags:

c#

forms

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.

like image 589
user2418900 Avatar asked Jan 11 '16 08:01

user2418900


2 Answers

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.

like image 112
zdimension Avatar answered Nov 15 '22 15:11

zdimension


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.

like image 24
Mohit S Avatar answered Nov 15 '22 15:11

Mohit S