vb.netmultithreadingparametersinvokeaddress-operator

vb.net passing variables in shared subs


Public Shared _formRef As frmMain
Public Shared f As frmMain

    Class Server
        <STAThread()> Public Shared Sub Main()
            Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
            Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
            Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
            Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
            AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

            aStringMessageReceiver.AttachInputChannel(anInputChannel)
        End Sub

        Private Shared Sub StringMessageReceived(ByVal sender As Object, ByVal e As StringMessageEventArgs)
            _formRef = f

            LANResponse = Convert.ToString(e.Message)
            Dim lanSent As String() = Nothing
            Dim sep(3) As Char
            Dim s As String = ""

            sep(0) = "~"
            'sep(1) = ","
            lanSent = LANResponse.Split(sep, 2)

            Dim a As New Threading.Thread(AddressOf getStatus)

            a.SetApartmentState(Threading.ApartmentState.STA)
            a.Start(Trim(lanSent(0)) & Trim(lanSent(1)))
   End Sub
End Class

Class Sending
        Public Shared Sub Main(ByRef whatToWrite As String)
            Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
            Dim anOutputChannel As IOutputChannel = aTcpMessaging.CreateOutputChannel(theIPforVM & ":" & thePort)
            Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
            Dim aStringMessageSender As IStringMessageSender = aStringMessagesFactory.CreateStringMessageSender()

            aStringMessageSender.AttachOutputChannel(anOutputChannel)
            aStringMessageSender.SendMessage(whatToWrite)
        End Sub
End Class

    Private Shared Sub getStatus(ByVal data As Object)
        _formRef.UpdateLabelText("Static: " & data)
    End Sub

    Public Sub UpdateLabelText(ByVal text As String)
        If Me.lblStatus.InvokeRequired Then
            Me.lblStatus.Invoke(New Action(Of String)(AddressOf UpdateLabelText), New Object() {text})
        Else
            Me.lblStatus.Text = text
        End If
    End Sub

#Region "Form Load"
    <STAThread()> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call Server.Main()
        '--------------------------------
        isTesting = False
        lblVer.Text = "V.7"
        '--------------------------------
    End Sub
#End Region

but once firing off the code i get the error you see in my image attached

error

But oddly enough i do pass the value i am looking for:

error 2


Solution

  • You never assign the running instance of frmMain to the shared variables.

    If you change your Server Main method to:

        <STAThread()> Public Shared Sub Main(formRef As frmMain)
            f = FormRef
    

    and then change your call to this method to:

    Call Server.Main(Me)
    

    Then that should solve your problem (assuming Server.Main is called from frmMain).