VB无法从一个方法访问另一个类的实例,但可以从另一个方法访问

时间:2014-08-21 08:36:48

标签: vb.net instance

我有两个主要课程。第一个启动第二个实现FIX dll。我希望能够在第二个中的第一个中设置variables / textbox.text。我可以在第二种方法中使用te OnCreate方法执行此操作,但出于某种原因无法使用OnLogon方法。有什么想法吗?

由于

Imports QuickFix

Public Class GlobalVariable
    Inherits MainFIXClass

    Public Shared instance As MainFIXClass
End Class


Public Class MainFIXClass

    Private Sub CurrencyArb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GlobalVariable.instance = Me
        Connect({"D:\\Config.txt"})
    End Sub

    Private Sub Connect(args As String())
        Dim settings As New QuickFix.SessionSettings(args(0))
        Dim myApp As IApplication = New MyQuickFixApp()
        Dim storeFactory As QuickFix.IMessageStoreFactory = New FileStoreFactory(settings)
        Dim logFactory As QuickFix.ILogFactory = New FileLogFactory(settings)
        Dim initiator As New QuickFix.Transport.SocketInitiator(myApp, storeFactory, settings, logFactory)

        Try
            initiator.Start()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


End Class

Public Class MyQuickFixApp
    Inherits MainFIXClass
    Implements IApplication

    Private _session As Session = Nothing
    Private _settings As QuickFix.Session = Nothing
    Private _Store As QuickFix.IMessageStore = Nothing
    Private _instance As MainFIXClass

    Public Sub OnCreate(sessionID As SessionID) Implements IApplication.OnCreate
        _instance = GlobalVariable.instance
        _session = Session.LookupSession(sessionID)
        _instance.OutputTB.Text = "On Create - " & sessionID.ToString & "   " & _session.ToString              ' **************************** this line works
    End Sub

    Public Sub OnLogon(sessionID As SessionID) Implements IApplication.OnLogon
        _instance.OutputTB.Text = "Logged On"        ' **************************** this line does not work ?????
        MsgBox("Logged On")
    End Sub

End Class

3 个答案:

答案 0 :(得分:0)

要阻止Window阻止,可以使用Threads或BackgroundWorker。我准备了一个小代码片段,演示了如何使用BackgroundWorker。请注意DoWork和WorkCompleted方法如何使用Handles连接到worker实例。

Imports System.ComponentModel
Imports System.Threading
Imports QuickFix
Imports QuickFix.Fields
Imports QuickFix.MessageCracker

Public Class MainFIXClass
  Implements IApplication

  Private WithEvents bw As BackgroundWorker

  Private _session As Session = Nothing
  Private _settings As QuickFix.Session = Nothing
  Private _Store As QuickFix.IMessageStore = Nothing

#Region "IApplication Methods"

  Public Sub OnCreate(sessionID As SessionID) Implements IApplication.OnCreate
    _session = Session.LookupSession(sessionID)
    Me.ShowOutput("On Create - " & sessionID.ToString & "   " & _session.ToString)
  End Sub

  Public Sub OnLogon(sessionID As SessionID) Implements IApplication.OnLogon
    Me.ShowOutput("Logged On")
  End Sub

#End Region

  Private Sub CurrencyArb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    bw = New BackgroundWorker
    bw.WorkerReportsProgress = True
    bw.RunWorkerAsync()
  End Sub

  Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork
    Dim settings As New QuickFix.SessionSettings("D:\\CurrencyArb.txt")
    Dim myApp As IApplication = New MyQuickFixApp()
    Dim storeFactory As QuickFix.IMessageStoreFactory = New FileStoreFactory(settings)
    Dim logFactory As QuickFix.ILogFactory = New FileLogFactory(settings)
    Dim initiator As New QuickFix.Transport.SocketInitiator(myApp, storeFactory, settings, logFactory)

    bw.ReportProgress(0, "Starting initiator")
    initiator.Start()
    bw.ReportProgress(0, "Initiator started successfully")

  End Sub

  Private Sub bw_ReportProgress(source As Object, e As ProgressChangedEventArgs) Handles bw.ProgressChanged
    Me.ShowOutput(e.UserState.ToString())
  End Sub

  Private Sub bw_WorkCompleted(source As Object, e As RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
    If e.Error IsNot Nothing Then
      'and exception occured
      MsgBox(e.Error.Message)
    Else
      'everything was fine, you can use the result
      Dim result As Object = e.Result
    End If
    Me.ShowOutput("Worker finished")
  End Sub

  Public Sub ShowOutput(msg As String)
    Me.OutputTB.Text = msg
  End Sub

End Class

答案 1 :(得分:0)

Imports System.ComponentModel
Imports System.Threading
Imports QuickFix
Imports QuickFix.Fields
Imports QuickFix.MessageCracker

Public Class MainFIXClass
    Private WithEvents bw As BackgroundWorker

    Delegate Sub ShowOutputDelegate(value As String)

    Private Sub CurrencyArb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GlobalVariable.instance = Me
        bw = New BackgroundWorker
        bw.RunWorkerAsync()
    End Sub

    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork
        Dim settings As New QuickFix.SessionSettings("D:\\CurrencyArb.txt")
        Dim myApp As IApplication = New MyQuickFixApp()
        Dim storeFactory As QuickFix.IMessageStoreFactory = New FileStoreFactory(settings)
        Dim logFactory As QuickFix.ILogFactory = New FileLogFactory(settings)
        Dim initiator As New QuickFix.Transport.SocketInitiator(myApp, storeFactory, settings, logFactory)
        Try

            'If Not TypeOf e.Argument Is String() Then 'only take valid input
            '    Throw New ArgumentException("Invalid argument")
            'End If
            'Dim args As String() = DirectCast(e.Argument, String()) 'you can pass your arguments here

            initiator.Start()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub bw_WorkCompleted(source As Object, e As RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
        If e.Error IsNot Nothing Then
            'and exception occured
        Else
            'everything was fine, you can use the result
            Dim result As Object = e.Result
        End If
    End Sub

    Public Sub ShowOutput(msg As String)
        If InvokeRequired Then
            Invoke(New ShowOutputDelegate(AddressOf ShowOutput), msg)
        Else
            Me.OutputTB.Text = msg
        End If
        Me.OutputTB.Text = msg
    End Sub

End Class

Public Class MyQuickFixApp
    Implements IApplication

    Private _session As Session = Nothing
    Private _settings As QuickFix.Session = Nothing
    Private _Store As QuickFix.IMessageStore = Nothing
    Private _instance As MainFIXClass

    Public Sub OnCreate(sessionID As SessionID) Implements IApplication.OnCreate
        _instance = GlobalVariable.instance
        _session = Session.LookupSession(sessionID)
        _instance.ShowOutput("On Create - " & sessionID.ToString & "   " & _session.ToString)
    End Sub

    Public Sub OnLogon(sessionID As SessionID) Implements IApplication.OnLogon
        _instance.ShowOutput("Logged On")
    End Sub

End Class

Public Class GlobalVariable
    Public Shared instance As MainFIXClass
End Class

答案 2 :(得分:0)

这是合并两个类的片段

Imports System.ComponentModel
Imports System.Threading
Imports QuickFix
Imports QuickFix.Fields
Imports QuickFix.MessageCracker

Public Class MainFIXClass
  Implements IApplication

  Private WithEvents bw As BackgroundWorker

  Private _session As Session = Nothing
  Private _settings As QuickFix.Session = Nothing
  Private _Store As QuickFix.IMessageStore = Nothing

#Region "IApplication Methods"

  Public Sub OnCreate(sessionID As SessionID) Implements IApplication.OnCreate
    _session = Session.LookupSession(sessionID)
    Me.ShowOutput("On Create - " & sessionID.ToString & "   " & _session.ToString)
  End Sub

  Public Sub OnLogon(sessionID As SessionID) Implements IApplication.OnLogon
    Me.ShowOutput("Logged On")
  End Sub

#End Region

  Private Sub CurrencyArb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    bw = New BackgroundWorker
    bw.WorkerReportsProgress = True
    bw.RunWorkerAsync()
  End Sub

  Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork
    Dim settings As New QuickFix.SessionSettings("D:\\CurrencyArb.txt")
    Dim myApp As IApplication = New MyQuickFixApp()
    Dim storeFactory As QuickFix.IMessageStoreFactory = New FileStoreFactory(settings)
    Dim logFactory As QuickFix.ILogFactory = New FileLogFactory(settings)
    Dim initiator As New QuickFix.Transport.SocketInitiator(myApp, storeFactory, settings, logFactory)

    bw.ReportProgress(0, "Starting initiator")
    initiator.Start()
    bw.ReportProgress(0, "Initiator started successfully")

  End Sub

  Private Sub bw_ReportProgress(source As Object, e As ProgressChangedEventArgs) Handles bw.ProgressChanged
    Me.ShowOutput(e.UserState.ToString())
  End Sub

  Private Sub bw_WorkCompleted(source As Object, e As RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
    If e.Error IsNot Nothing Then
      'and exception occured
      MsgBox(e.Error.Message)
    Else
      'everything was fine, you can use the result
      Dim result As Object = e.Result
    End If
    Me.ShowOutput("Worker finished")
  End Sub

  Public Sub ShowOutput(msg As String)
    Me.OutputTB.Text = msg
  End Sub

End Class