vb.net多线程activex

时间:2014-11-29 00:39:18

标签: vb.net multithreading

好的,我已经看过一大堆多线程的例子;所有都在表单上显示可爱的小计数器,而您仍然可以使用backgroundworker或线程池访问表单。我知道了。我没有得到的是将它应用到现实世界的应用程序中。 这是我的应用程序,我不知道如何多线程这个; 通过第三方ActiveX API,我以高速率将记录读入SQL表;每天8小时内有数百万条记录。 当记录的流式传输发生时,我需要查看记录,进行一些计算并应用过滤器,并实时表示结果。 (将其过滤到大约100条记录/天) 我猜你已经看到了我要去的地方; 如果我在流式传输记录时动态进行计算/过滤;它导致流延迟并且我松开连接,所以我需要多线程完成这两个任务。

流入SQL表的过程如下:(AxTDAAPIComm是ActiveX) 在Form1上:

    Private Sub SubscrBTN_Click(sender As Object, e As EventArgs) Handles SubscrBTN.Click
    AxTDAAPIComm1.Subscribe(StreamOL, tdaactx.TxTDASubTypes.TDAPI_SUB_L1)
End Sub
Private Sub AxTDAAPIComm1_OnL1Quote(sender As Object, e As Axtdaactx.ITDAAPICommEvents_OnL1QuoteEvent) Handles AxTDAAPIComm1.OnL1Quote
    SQL.AddStream(e.quote.Symbol, e.quote.Bid, e.quote.Ask, e.quote.Last, e.quote.PrevClose, e.quote.Volume, e.quote.TradeTime, e.quote.QuoteTime, e.quote.TradeDate, e.quote.QuoteDate, e.quote.Volatility, e.quote.OpenInterest, e.quote.UnderlyingSymbol, e.quote.CallPut, e.quote.LastSize, e.quote.MH_LastSize, e.quote.MH_IsQuote, e.quote.MH_IsTrade)
End Sub
End Class

单击该按钮,调用AxTDAAPIComm1_OnL1Quote子,该子流回送记录。 然后在我的SQL类中,我将它发送到我的SQL表:

    Public Sub AddStream(Symbol As String, Bid As Single, Ask As Single, Last As Single, PrevClose As Single, Volume As Integer, TradeTime As Integer, QuoteTime As Integer, TradeDate As Integer, Quotedate As Integer, Volatility As Single, OpenInterest As Integer, UnderlyingSymbol As String, CallPut As String, LastSize As Integer, MH_LastSize As Integer, MH_IsQuote As Boolean, MH_IsTrade As Boolean)
    Try
        Dim TradeAmount As Single = 0
        Dim Trade As String = ""
        TradeAmount = LastSize * Last * 100
        Select Case Last
            Case Is = Ask
                Trade = "Ask"
            Case Is > Ask
                Trade = "Above Ask"
            Case Is = Bid
                Trade = "Bid"
            Case Is < Bid
                Trade = "Below Bid"
            Case Else
                Trade = "Mid"
        End Select


        Dim strStream As String = "INSERT INTO OptionStream (Symbol,Bid,Ask,Last,PrevClose,Volume,TradeTime,QuoteTime,TradeDate,QuoteDate,Volatility,OpenInterest,UnderlyingSymbol,CallPut,TradeAmount,Trade,LastSize,MH_LastSize,MH_IsQuote,MH_IsTrade) " & _
                                  "VALUES (" & _
                                  "'" & Symbol & "'," & _
                                  "'" & Bid & "'," & _
                                  "'" & Ask & "'," & _
                                  "'" & Last & "'," & _
                                  "'" & PrevClose & "'," & _
                                  "'" & Volume & "'," & _
                                  "'" & TradeTime & "'," & _
                                  "'" & QuoteTime & "'," & _
                                  "'" & TradeDate & "'," & _
                                  "'" & Quotedate & "'," & _
                                  "'" & Volatility & "'," & _
                                  "'" & OpenInterest & "'," & _
                                  "'" & UnderlyingSymbol & "'," & _
                                  "'" & CallPut & "'," & _
                                  "'" & TradeAmount & "'," & _
                                  "'" & Trade & "'," & _
                                  "'" & LastSize & "'," & _
                                  "'" & MH_LastSize & "'," & _
                                  "'" & MH_IsQuote & "'," & _
                                  "'" & MH_IsTrade & "') "


        SQLCon.Open()
        SQLCmd = New SqlCommand(strStream, SQLCon)
        SQLCmd.ExecuteNonQuery()
        SQLCon.Close()


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

End Sub

在我的表单上,我创建了一个文本框,用于输入SQL查询语句,我按如下方式捕获:

   Private Sub cmdQuery_Click(sender As Object, e As EventArgs) Handles cmdQuery.Click
    If txtQuery.Text <> "" Then
        If SQL.HasConnection = True Then
            SQL.RunQueryWL(txtQuery.Text)
            If SQL.SQLDatasetWL.Tables.Count > 0 Then
                DGVData.DataSource = SQL.SQLDatasetWL.Tables(0)
            End If
        End If
    End If
End Sub

正如您所看到的,它现在转到SQL类并运行Query:

    Public Function HasConnection() As Boolean
    Try
        SQLCon.Open()
        SQLCon.Close()
        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try
End Function
Public Sub RunQueryWL(Query As String)
    Try
        SQLCon.Open()
        SQLCmd = New SqlCommand(Query, SQLCon)
        SQLDA = New SqlDataAdapter(SQLCmd)
        SQLDatasetWL = New DataSet
        SQLDA.Fill(SQLDatasetWL)
        SQLCon.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
        If SQLCon.State = ConnectionState.Open Then
            SQLCon.Close()
        End If
    End Try
End Sub

,结果显示在我的表单上的数据网格视图中。 您可以想象,查询几百万条记录需要几秒钟,因此我想将AxTDAAPIComm1.Subscribe和AxTDAAPIComm1_OnL1Quote和RunQueryWL Subs放在不同的线程上。

我该怎么做?

0 个答案:

没有答案