How to wait for form to close without ShowDialog()?

.net, oauth, showdialog, vb.net, webbrowser-control

Solution

Do not use a Timer. Events are made for this. You have to subscribe to the FormClosing event of your Form. The code that should be executed after the window is closed has to be in the event handler method.

Use the AddHanlder keyword.

AddHandler yourForm.FormClosing, AddressOf FormClosingEventhandler

Later use the event handler to proceed:

Private Sub FormClosingEventhandler()
    'proceed here
End Sub

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx

Problem

In my program I use WebBrowser control for OAuth. It's located on `vk_auth_window` form. So in the code I call `vk_auth_window.WebBrowser1.Navigate(vkauthurl)`. If authentication is successful, I silently get token, but if user is not authenticated a form `vk_auth_window` is showed to them to enter username and password. To check this I catch _DocumentComplete event and check for the right URL. I need no other code being executed until the user finished authentication or closed authentication form. ShowDialog() does the trick, but it somehow hides cursor from username and password fields in WebBrowser control. So I introduced a variable `Dim showform as Boolean=True` in and set it to False from the _DocumenComplete event and in the main code I use infinite loop. ``` While showform System.Threading.Thread.Sleep(100) Application.DoEvents() End While ``` However it makes userinput slow due to thread sleeps. And If i remove them, it loads CPU quite a lot. Is there a better way to wait for form to close? What is it?

Original source