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,
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