vb.net在共享subs中传递变量

时间:2012-04-23 00:14:27

标签: vb.net multithreading parameters invoke address-operator

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

但是一旦解雇了代码,我就会收到你在附件中看到的错误

error

但奇怪的是,我确实传递了我正在寻找的价值:

error 2

1 个答案:

答案 0 :(得分:1)

您永远不会将正在运行的frmMain实例分配给共享变量。

如果您将Server Main方法更改为:

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

然后将您对此方法的调用更改为:

Call Server.Main(Me)

然后这应该可以解决您的问题(假设从frmMain调用Server.Main)。