0

我是股票交易应用程序的新手,自过去 9 个月以来我一直在处理此应用程序vb.net

我成功地在其中显示实时数据listview并且我曾经threadpool完成此任务但是当应用程序显示实时股票报价时,系统 CPU 使用率变高(大约 30 到 45%)所以我怎样才能减少趋于 0 到 5%

我访问了这个链接Real-Time-Data-Grid但我对这个链接并不完全满意(但它只对有用C#)所以任何完美的建议在vb.net.?

在我的应用程序中,我正在从第三方软件的 excel 表中读取实时数据。所以我想知道这是在重载过程中读取COM 。?vb.net

或者如果有任何其他关于免费实时勾选印度股票市场数据 API 的建议,谷歌金融雅虎金融除外

这是代码:

我为名为RTDFeed.vb的实时数据馈送定义了类

Imports System.Data.OleDb
Imports Microsoft.Win32
Imports System.Security
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Text
Imports System.Security.AccessControl
Imports System.Threading
Imports System.ComponentModel
Imports System.Windows
Imports System.Windows.Forms
Imports System.Drawing.Color
Imports System.Net
Imports System.Data.SqlClient
Imports System.Drawing

Public Class RTDFeed
    ''Class varialbal Data
    Private SyncRoot As New Object()
    Private _numRow As Integer = 0
    Private _stTime As DateTime
    Private _EndTime As DateTime
    Private _Exchg As String = String.Empty
    Private _Description As String = String.Empty
    Private _ScpCode As String = String.Empty
    Private _Connfrom As String = String.Empty
    Private _IsRunning As Boolean = False
    Private _EventStopped As EventWaitHandle


    ''Delegates and Event
    Public Event OnRowUpdate As RowUpdateEventHandler
    Public Delegate Sub RowUpdateEventHandler(ByVal sender As System.Object, ByVal e As RowUpdateEventArgs)

    Public Event OnStarted As OnStartedHandler
    Public Delegate Sub OnStartedHandler(ByVal Sender As System.Object, ByVal e As EventArgs)

    Public Event OnStopped As OnStoppedHandler
    Public Delegate Sub OnStoppedHandler(ByVal Sender As System.Object, ByVal e As EventArgs)


    ''Public constructor
    Public Sub New(ByVal numrow As Integer, ByVal Excg As String, ByVal Description As String, ByVal ConnFrom As String, ByVal ScpCode As String)
        Me._numRow = numrow
        Me._Exchg = Excg
        Me._Description = Description
        Me._ScpCode = ScpCode
        Me._Connfrom = ConnFrom
        Me._EventStopped = New ManualResetEvent(False)
    End Sub

    Public Sub New(ByVal numrow As Integer, ByVal Excg As String, ByVal Description As String, ByVal ConnFrom As String)
        Me._numRow = numrow
        Me._Exchg = Excg
        Me._Description = Description
        Me._ScpCode = ScpCode
        Me._Connfrom = ConnFrom
        Me._EventStopped = New ManualResetEvent(False)
    End Sub


    ''Public Method
    Public Sub StartProc()
        SyncLock Me.SyncRoot
            If Not Me._IsRunning Then
                Me._EventStopped.Reset()
                ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Me.ThreadGetstockproc))
            End If
        End SyncLock
    End Sub

    Public Sub StopAsync()
        Dim Ts As New ThreadStart(AddressOf Me.StopProc)
        Dim Thd As New Thread(Ts)
        Thd.Start()
    End Sub


    ''Private Method
    Private Sub StopProc()
        SyncLock Me.SyncRoot
            If Me._IsRunning Then
                Me._IsRunning = False
                Me._EventStopped.WaitOne()
                RaiseEvent OnStopped(Me, EventArgs.Empty)
            End If
        End SyncLock
    End Sub

    Private Sub ThreadGetstockproc(ByVal stateinfo As Object)
        Me._IsRunning = True
        RaiseEvent OnStarted(Me, EventArgs.Empty)

        Try
            While Not Thread.CurrentThread.ThreadState = ThreadState.AbortRequested
                'Me._stTime = DateTime.Now
                If Me.GetStock.Count > 0 Then
                    RaiseEvent OnRowUpdate(Nothing, New RowUpdateEventArgs(Me._numRow, Me.GetStock))
                End If

                'Dim Ts As TimeSpan = Me._EndTime - Me._stTime
                'Dim delay As Integer = Ts.Milliseconds

                Thread.Sleep(250) 'delay)

                If Not Me._IsRunning Then
                    Thread.CurrentThread.Abort()
                End If
            End While

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Me._EventStopped.Set()
        End Try
    End Sub

    Dim i As Double = 0
    Private Function GetStock() As List(Of Double)

        Dim CelUpdt As New List(Of Double)
        Dim Querystr As String = ""
        Dim cmd As OleDbCommand
        Dim Chang As Double

        i += 1

        CelUpdt.Clear()


            Querystr = "Select F1,F2,F4,F5,F6,F7,F12,F15,F16,F18 From [Sheet1$] Where Trim(F1)='" & Me._Exchg & "' And Trim(F2)='" & Me._Description & "' And Trim(F3)='" & Me._ScpCode & "'"

            cmd = New OleDbCommand(Querystr, Excelcn)
            Dim FReader As OleDbDataReader
            FReader = cmd.ExecuteReader()

            If FReader.HasRows Then
                FReader.Read()

                'Market Value
                CelUpdt.Add(CDbl(Val(FReader.Item("F4"))))

                CelUpdt.Add(CDbl(Val(FReader.Item("F5"))))

                CelUpdt.Add(CDbl(Val(FReader.Item("F6"))))

                CelUpdt.Add(CDbl(Val(FReader.Item("F7"))))

                CelUpdt.Add(i) 'CDbl(Val(FReader.Item("F12"))))

                CelUpdt.Add(CDbl(Val(FReader.Item("F15"))))

                CelUpdt.Add(CDbl(Val(FReader.Item("F16"))))

                Chang = ((CDbl(FReader.Item("F12")) - CDbl(FReader.Item("F18"))) / CDbl(FReader.Item("F12"))) * 100
                CelUpdt.Add(Chang)

                FReader.Close()
            End If

        'Me._EndTime = DateTime.Now
        Return CelUpdt

    End Function

    ''Class property
    Public Property Numrow() As Integer
        Get
            Return Me._numRow
        End Get
        Set(ByVal value As Integer)
            Me._numRow = value
        End Set
    End Property

    Public Property Exchg() As String
        Get
            Return Me._Exchg
        End Get
        Set(ByVal value As String)
            Me._Exchg = value
        End Set
    End Property

    Public Property Desciption() As String
        Get
            Return Me._Description
        End Get
        Set(ByVal value As String)
            Me._Description = value
        End Set
    End Property

    Public Property ScpCode() As String
        Get
            Return Me._ScpCode
        End Get
        Set(ByVal value As String)
            Me._ScpCode = value
        End Set
    End Property

    Public Property ConnFrom() As String
        Get
            Return Me._Connfrom
        End Get
        Set(ByVal value As String)
            Me._Connfrom = value
        End Set
    End Property

    Public Property Isrunning() As Boolean
        Get
            Return Me._IsRunning
        End Get
        Set(ByVal value As Boolean)
            Me._IsRunning = value
        End Set
    End Property

End Class

Public Class RowUpdateEventArgs
    Inherits System.EventArgs

    ''class variabal Data
    Private _ActiveRow As Integer
    Private _CellCollection As New List(Of Double)

    ''Public Constructor
    Public Sub New(ByVal ActRow As Integer, ByVal CellArray As List(Of Double))
        _ActiveRow = ActRow
        _CellCollection = CellArray
    End Sub

    ''Public Property
    Public Property ActiveRow() As Integer
        Get
            Return Me._ActiveRow
        End Get
        Set(ByVal value As Integer)
            Me._ActiveRow = value
        End Set
    End Property

    Public Property CellCollection() As List(Of Double)
        Get
            Return Me._CellCollection
        End Get
        Set(ByVal value As List(Of Double))
            Me._CellCollection = value
        End Set
    End Property

End Class

在主手表上,我更新 UI 线程意味着更新 listview Cell onOnRowupdateEventArgs

主表形式其名为watch.vb

Imports System.Data.OleDb
Imports Microsoft.Win32
Imports System.Security
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Text
Imports System.Security.AccessControl
Imports System.Threading
Imports System.ComponentModel
Imports System.Windows
Imports System.Windows.Forms
Imports System.Drawing.Color
Imports System.Net
Imports System.Data.SqlClient
Imports System.Drawing

Public Class FrmWatch

Private Sub FrmWatch_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.MdiParent = FrmMainscreen
            Me.Icon = FrmMainscreen.Icon
            Me.Width = FrmMainscreen.Width - 13
            'Me.DoubleBuffered = True

            LoadBackgroundWorker.WorkerReportsProgress = True
            LoadBackgroundWorker.WorkerSupportsCancellation = True
            FrmMainscreen.submnubuyorder.Enabled = True
            FrmMainscreen.subnusellorder.Enabled = True
            FrmMainscreen.submnupendingorder.Enabled = True
            FrmMainscreen.ConformOrderTradeBookToolStripMenuItem.Enabled = True
            FrmMainscreen.MktPicToolStripMenuItem.Enabled = True


            ''Load Market Column created
            LoadFileFormat()
            LVW.Items.Clear()
            LVW.Buffer()

            LVW.Columns.Add("Exchang", Clm_exchg, HorizontalAlignment.Left)
            LVW.Columns.Add("Symbol", Clm_scrpt, HorizontalAlignment.Left)
            LVW.Columns.Add("Ser/Exp", Clm_ExpDT, HorizontalAlignment.Left)

            LVW.Columns.Add("Buy Qty", Clm_bqty, HorizontalAlignment.Right)
            LVW.Columns.Add("Buy Price", Clm_bPric, HorizontalAlignment.Right)
            LVW.Columns.Add("Sell Price", Clm_spric, HorizontalAlignment.Right)
            LVW.Columns.Add("Sell Qty", Clm_sqty, HorizontalAlignment.Right)
            LVW.Columns.Add("Last Traded Price", Clm_ltPtic, HorizontalAlignment.Right)
            LVW.Columns.Add("High", Clm_high, HorizontalAlignment.Right)
            LVW.Columns.Add("Low", Clm_low, HorizontalAlignment.Right)
            LVW.Columns.Add("Open", Clm_open, HorizontalAlignment.Right)
            LVW.Columns.Add("Close", Clm_close, HorizontalAlignment.Right)
            LVW.Columns.Add("%Change", Clm_chg, HorizontalAlignment.Right)
            LVW.Columns.Add("Trand", Clm_Trnd, HorizontalAlignment.Center)
            LVW.Columns.Add("Scrip Code", Clm_ScpCode, HorizontalAlignment.Left)

            LVW.SuspendLayout()
            ''call backgroundworker for Load Mkt
            LoadBackgroundWorker.RunWorkerAsync()


            If FrmPendingOrder.Visible = True Then
                AddHandler Me.UpdatePendingOrd, AddressOf FrmPendingOrder.UpdatePendingOrderTimerFilter
            End If
            If FrmConformOrder.Visible = True Then
                AddHandler Me.UpdateConformOrd, AddressOf FrmConformOrder.RefreshTrade
            End If

        Catch ex As Exception
            ErrorHandler(ex, ex.StackTrace, Reflection.MethodBase.GetCurrentMethod.ToString)
        End Try
    End Sub

Private Sub LoadBackgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles LoadBackgroundWorker.DoWork
        Try
                 LoadMarket()
        Catch ex As Exception
            ErrorHandler(ex, ex.StackTrace,Reflection.MethodBase.GetCurrentMethod.ToString)
        End Try
    End Sub

Private Sub LoadBackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LoadBackgroundWorker.RunWorkerCompleted
        Try
            LVW.ResumeLayout()
            If LVW.Items.Count > 0 Then
                LVW.Focus()
                Me.LVW.Items(0).Selected = True
            End If

For i As Integer = 0 To Me.LVW.Items.Count - 1
                        If Me.LVW.Items(i).Text <> String.Empty And Me.LVW.Items(i).SubItems(1).Text <> String.Empty Then
                            Dim RTDF As New RTDFeed(i, Me.LVW.Items(i).Text, FeedDesc, CnFrm, Me.LVW.Items(i).SubItems(14).Text)
                            AddHandler RTDF.OnRowUpdate, AddressOf Me.OnRTDFeedRowUpdat
                            RTDFeed_Obj.Add(RTDF)
                            MAX_FEED += 1
                        End If
                    Next

                    For j As Integer = 0 To MAX_FEED - 1
                        If Not RTDFeed_Obj(j).Isrunning Then
                            RTDFeed_Obj(j).StartProc()
                        End If
                    Next
        Catch ex As Exception
            ErrorHandler(ex, ex.StackTrace,Reflection.MethodBase.GetCurrentMethod.ToString)
        End Try
    End Sub

Private Delegate Sub OnRTDFeedRowUpdateHandler(ByVal sender As System.Object, ByVal e As RowUpdateEventArgs)
    Private Sub OnRTDFeedRowUpdat(ByVal sender As System.Object, ByVal e As RowUpdateEventArgs)
        If Me.InvokeRequired Then
            Me.Invoke(New OnRTDFeedRowUpdateHandler(AddressOf Me.OnRTDFeedRowUpdat), New Object() {sender, e})
            Return
        End If
        RowUpdate(e)
    End Sub

    Private Sub RowUpdate(ByVal e As RowUpdateEventArgs)
        SyncLock Me.SyncRoot

            Try
                'LVW.Items(e.ActiveRow).SubItems(3).Text = e.CellCollection(0).ToString
                'LVW.Items(e.ActiveRow).SubItems(4).Text = CellArray.Item(1).ToString()
                'LVW.Items(e.ActiveRow).SubItems(6).Text = CellArray.Item(2).ToString()
                'LVW.Items(e.ActiveRow).SubItems(5).Text = CellArray.Item(3).ToString()
                LVW.Items(e.ActiveRow).SubItems(7).Text = e.CellCollection(4).ToString 'CellArray.Item(4).ToString()
                'LVW.Items(e.ActiveRow).SubItems(8).Text = CellArray.Item(5).ToString()
                'LVW.Items(e.ActiveRow).SubItems(9).Text = CellArray.Item(6).ToString()
                'LVW.Items(e.ActiveRow).SubItems(12).Text = CellArray.Item(7).ToString()
            Catch ex As IndexOutOfRangeException
                MsgBox(ex.Message)
            End Try

        End SyncLock
    End Sub

现在我的自定义 Listview 类名为My_Grid,它在实时更新单元格时是无闪烁的列表视图。

Public Class My_GRID
    Inherits ListView
    Public Sub Buffer()
        Me.DoubleBuffered = True
    End Sub
End Class
4

0 回答 0