具有单个Datagridview的多个计时器线程

时间:2019-06-26 14:42:05

标签: vb.net multithreading datagridview timer visual-studio-2017

我在Visual Studio 2017中使用VB.NET。

我正在测试具有datagridview的表单。该网格正在填充初始交易符号。加载交易品种后,我需要按启动按钮,这将启动一个timer1以从交易所寻找该交易品种的最新价格并启动timer2。如果满足条件,Timer2将寻找价格的某些条件,将下订单进行交换并再次寻找另一个循环。在这种情况下,timer1也以2秒的间隔运行,以获取最新的交易价格。

我为每个计时器使用两个不同的BackgroundWorker来完成网格的这两项任务。

这几乎可以正常工作,但是有时两个或两个计时器都被禁用。

就代码,设计和架构而言,我正在寻找另一种最佳的最佳方法。如果有人可以帮助。

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Me.Timer1.Stop()

    Try
        With Me.DataGridView4
            For RowIndex = 0 To .RowCount - 1
                With .Rows(RowIndex)
                    'CODE FOR CHECKING LTP OF SYMBOL
                End With
            Next
        End With
    Catch ex As Exception

    End Try
End Sub

Private Sub BackgroundWorker2_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker2.DoWork
    Me.Timer2.Stop()


    Try

        For Each row As DataGridViewRow In Me.DataGridView4.Rows

            Dim TRDSYM As String = row.Cells(1).Value
            Dim orderid As String = row.Cells(6).Value
            Dim Ltp As Decimal = row.Cells(2).Value
            Dim EntryPrice As Decimal = Ltp

            'PLACING AN ORDER TO EXCHANGE BASED ON CONDITION
        Next
    Catch ex As Exception

    End Try
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'SetLog("Market Watch For Ltp Background Worker going on...", LogType.Others)
    BackgroundWorker1.RunWorkerAsync()

End Sub

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    'SetLog("Order Placement Algorithm Background Worker going on...", LogType.Others)
    BackgroundWorker2.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        If Me.Timer2.Enabled = False Then
            Timer2.Enabled = True
            Me.Timer2.Start()
            Me.btnOrderplacementstatus.Text = "Order Placement Running..."
            Me.btnOrderplacementstatus.BackColor = Color.Green
        End If
        'SetLog("BackgroundWorker 2 Order Placement Thread started...", LogType.Others)
        Me.Timer1.Enabled = True
        Me.Timer1.Start()
        Me.btnMarketwatchstatus.Text = "Market Watch Running..."
        Me.btnMarketwatchstatus.BackColor = Color.Green

    End If
End Sub

Private Sub BackgroundWorker2_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
    If Me.Timer2.Enabled = False Then
        Me.Timer2.Enabled = True
        Me.Timer2.Start()
        Me.btnOrderplacementstatus.Text = "Order Placement Running..."
        Me.btnOrderplacementstatus.BackColor = Color.Green
    End If
End Sub

0 个答案:

没有答案