Dimo Petkov
Dimo Petkov

Reputation: 13

Invoke form and freeze

My question is a little complicated:

I am having Socket server, and I have two forms. One is the main form for the server and the other one is a custom made messagebox. When a request comes to the server the main form has to invoke the second form (messagebox). When the main form is open for the first time everything works fine. But when I close the main form without closing the application and open it again and request comes to the server the custom made messagebox opens but it freezes and looks like this:

enter image description here

The Code for invoking:

Private Delegate Sub DInvoke(ByRef Frm As Form, ByVal Text As String, ByVal Title As String, ByVal Socket As Integer, ByVal DataX As String, ByVal ReqForProfil As Boolean)

Public Sub ShowMessage(ByRef Frm As Form, ByVal Text As String, ByVal Title As String, ByVal Socket As Integer, ByVal DataX As String, ByVal ReqForProfil As Boolean)
    If Frm.InvokeRequired Then
        Dim DT As New DInvoke(AddressOf ShowMessage)
        Frm.Invoke(DT, New Object() {Frm, Text, Title, Socket, DataX, ReqForProfil})
    Else
        Messegar = New MessageFrm(Text, Title, Socket, DataX, ReqForProfil, True, 30)
        Messegar.Show()
    End If
End Sub

Any help would be appreciated!

Upvotes: 1

Views: 1560

Answers (1)

Hans Passant
Hans Passant

Reputation: 942000

Debug it. Set a breakpoint on InvokeRequired. You know it should be true since the method is called from a I/O completion thread. The problem you describe is consistent with the property returning False. So the Invoke doesn't happen and the form is created on the worker thread instead of the UI thread. Where it is dead as a doornail since that thread isn't pumping a message loop.

Which is invariably caused by passing a bad form object. Like "Form1", a type instead of an object, a nasty VB.NET trap. Or passing "new Form1", a new instance of the form object instead of the one that user is looking at.

A Q&D fix is to pass Application.OpenForms(0) instead. A clean fix is to pass a valid form reference to the constructor of the class that works with the socket.

Upvotes: 4

Related Questions