IIRC, showModalDialog is a blocking call. That means the control wont return to the function that calls showModalDialog unless the dialog closes. So, if you do
The control won't go to the call to close until the dialog closes, and by then the dialog is already closed
Why do you need shoModalDialog? Do you want the popup to be always on top? You can try using showModelessDialog