Surely one of these options should satisfy you
1. This is simple. Just show the form and dispose it. It's not modal and it runs on your original UI thread. wait
is not a blocking call so there's no need for any additional threading. When the window is closed, Dispose it automatically called after some time. If you try to dispose after closing, there is a chance that it is already disposed and you will get the exception you are getting. See the stopwait
method at the bottom which handles this case.
Public Sub wait()
ld = New Wform()
ld.Show()
End Sub
2. This is blocking. Showing a dialog window prevents your other code to run. stopwait
needs to be called if you want to dispose, because a dialog window is not disposed automatically when closed. Until the form is closed you can't use your calling form.
Public Sub wait()
ld = New Wform()
ld.ShowDialog()
End Sub
3. For when wait
is being called from a non-UI thread. (1. and 2. can both use this method for UI thread safety.) It is generally good practice to do use the InvokeRequired/Invoke pattern when multiple threads are present in a UI application.
Public Sub wait()
If Me.InvokeRequired Then
Me.Invoke(New Action(AddressOf wait))
Else
ld = New Wform()
ld.Show()
End If
End Sub
ShowDialog
and Show
should satisfy what you are trying to do in most cases; to block or not to block.
In all cases, stopwait
looks the same.
Public Sub stopwait()
If ld IsNot Nothing Then
If ld.InvokeRequired Then
ld.Invoke(New Action(AddressOf stopwait))
Else
If Not ld.IsDisposed Then ld.Dispose()
End If
End If
End Sub