I have a mainForm and dialogForm. I want to show modal dialogForm for 1 second and it should close programmatically. I tried this code:
//onButtonClick()
{
dialogForm->ShowModal();
Sleep(1000);
dialogForm->ModalResult = mrOk;
dialogForm->Close();
}
I tried also without Sleep(1000);
, but it just doesn't close the form. How properly to close this form?
ShowModal()
returns when the modal form is closed. That is one of the key points about a form being modal. So, only after the form is closed does your program call Sleep()
.
Instead you need to add a timer, with interval of 1000ms, to the form. Start the timer when the form is shown. When the timer fires, close the form.
Note that you should never call Sleep()
from the UI thread because this stops the UI thread processing messages and thus makes the program become unresponsive.