无法弄清楚如何从DLL调用表单的过程

时间:2019-06-28 23:11:31

标签: vb.net

该项目使用DLL为通过命名管道进行通信的通用程序提供接口。当在DLL的管道线程上接收到数据时,我需要调用驻留在应用程序中Form上的过程。我已经尝试过各种方法,但似乎无济于事。 无法访问表单的“ Sub”。我假设我需要使用一个委托,但是DLL没有任何形式,并且我不知道在这种情况下如何使用委托。

以下代码摘录:

Public Sub Receive()
    'This is the Client's receiver for the client's request return
    'The Client's pipe name is passed to the local UDPserver in the request
    'This routine is on a seperate thread and is started prior to any 
    'communications to the local UDPserver 

    Dim RequestBytes(ByteSize) As Byte
    Dim RequestByteCount As Integer = 0
    Try
        doNotify = New NotifyDelegate(AddressOf NotifyClient)
        ClientReceive = New NamedPipeServerStream(ClientPipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous)

        ClientReceiveActive = True
        While EndOfService = False
            ClientReceive.WaitForConnection()
            RequestByteCount = ClientReceive.Read(RequestBytes, 0, ByteSize)
            'Strip the CrLf (13, 10) indicating end of bytes
            Array.Resize(RequestBytes, RequestByteCount - 2)
            If RequestByteCount > 0 Then
                'Convert bytes back to string
                ServerReturn = Encoding.ASCII.GetString(RequestBytes)
            End If
            Array.Resize(RequestBytes, ByteSize)

            If ServerReturn.ToUpper.IndexOf("!") <> -1 Then
                'A broadcast Notification 
                Owner.Invoke(doNotify, New Object() {ServerReturn})
                ServerReturnValid = False
            Else
                'Send_Request that the server has returned
                ServerReturnValid = True
            End If
            ClientReceive.Disconnect()
        End While
    Catch ex As ThreadAbortException
        'Prevent error message for thread abort
        Thread.ResetAbort()
        ClientReceive.Close()
        Err.Clear()
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "ClientPipe Receive")
        Err.Clear()
    End Try

    ClientReceiveActive = False
End Sub

Public Sub NotifyClient(ByVal Notification As String)
    'Server generated notifications are sent here via delegate doNotify
    Notification = Notification.Replace("!", String.Empty)
End Sub

Owner.Invoke(doNotify, New Object() {ServerReturn})之所以起作用是因为我在DLL中有一个允许调用的伪表格,并且它在DLL中调用了NotifyClient

这可行,但是我希望NotifyClient子对象位于实例化DLL且未在DLL中声明时标识的应用程序窗体上。

1 个答案:

答案 0 :(得分:0)

由于Sub中的Form不是静态的,因此需要一个实际的Form对象才能调用Sub

Dim frm As <FormClassName>
frm = New <FormClassName>
...
frm.Receive

如果Sub中的Form不使用Form的任何局部变量或函数,则可以声明Sub静态,然后可以调用却没有实际的Form对象。

Public Shared Sub Receive
    ...
End Sub

您可以这样称呼它而无需实际对象。

<FormClassName>.Receive