Dialog form does not close

    General discussion

  • Hi,
    I have some code in C# which takes some time to execute. Hence I did a "Please wait" dialog box at the beginning of the code. At the end I try to close it using both dialog.Close() and dialog.Visible=False but the form does not close. Why is this happening and what can I do to overcome it?

    Thanks and regards,

    Monday, February 23, 2009 10:53 AM

All replies

  • Post your code

    Monday, February 23, 2009 10:56 AM

  • private
     Form5 modifyTextWait = new Form5();  
    private void searchToolStripMenuItem_Click(object sender, EventArgs e)  
    if (MessageBox.Show("All text will be converted to lowercase and all punctuation, digits and symbols will be removed. Are you sure you want to continue?",  
    "KWord", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)  
    == DialogResult.Yes)  
    // heavy computation  
    Monday, February 23, 2009 11:02 AM
  • try Show()

    ShowDialog()    Shows the form as a modal dialog box with the currently active window set as its owner.(MSDN) - as long as dialog opened // heavy computation   code not executed

    (or maybe i'm wrong)
    Monday, February 23, 2009 11:29 AM
  • When u use statement    modifyTextWait.ShowDialog();  
    the statement shows the form as a dialogbox. 

    When the method "ShowDialog()" is called, the code following it is not executed until after the dialog box is closed. The program execution doesn't go to next line untill the dialogbox is closed, thus your form does not close.

    When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel.

    In your case the same can be acheived using Threading.
    One thread will do the  heavy computattion and other thread will be used to show the Dialog.
    On Completion of heavy computation. you can close the form by calling  (close or visible) of Dialog box opened up by other process.
    In this case u will have to take care of cross-threading.

    Note : Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application.

    For more information read MSDN

    Monday, February 23, 2009 11:54 AM