0

timerTicks fires every second, and runs sub CheckTableForNewRecords as long as IsCheckingTable = False.

Sub CheckTableForNewRecords determines if my SQL table has new records, if it does, it will run sub SQL.GetTrades on a new thread.

I need the property IsCheckingTable to change to False only when the threaded sub SQL.GetTrades is finished.

How do I do this?

Public Sub timerTicks() Handles EventTimer.Tick
    If IsCheckingTable = False Then
        CheckTableForNewRecords()
    End If
    Timertxt.Text = Date.Now
End Sub

Public Sub CheckTableForNewRecords()
    sw.Restart()
    sw.Start()
    IsCheckingTable = True
    PR = LR
    SQL.GETlastrcrd(QueryLR, LR)
    NR = LR - PR
    LastrcrdFRM.Text = LR
    PrvLastrcrdFRM.Text = PR
    NewRecordsFRM.Text = NR
    'check table
    If NR > 0 Then
        Dim QueryRR As String = "SELECT * from(select ROW_NUMBER() over (Order by StreamingID ) as row, " & _
                                "StreamingID,WatchlistID,symbol,Bid,Ask,Last,High,Low,PrevClose,Volume,TradeTime," & _
                                "QuoteTime,TradeDate,QuoteDate,Volatility,OpenInterest,UnderlyingSymbol,CallPut,TradeVolume,TradeAmount,Trade,LastSize from optionstream) " & _
                                "as StreamingID where StreamingID between " & PR & " AND " & LR
        Threading.ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf SQL.GetTrades), QueryRR)
    End If
    IsCheckingTable = False
    TEchecktradesFRM.Text = sw.ElapsedMilliseconds
End Sub
Jeff B
  • 8,572
  • 17
  • 61
  • 140
PtheGr8
  • 17
  • 7

1 Answers1

0

Assuming you didn't care about thread-safety or anything, a very naive way you could do it would be something like:

ThreadPool.QueueUserWorkItem(
    Sub() 
        SQL.GetTrades()
        IsCheckingTable = True
    End Sub)

You may want to mark IsCheckingTable volatile for the reasons described here. You may also want a try/catch/finally inside the QueueUserWorkItem to make sure we reset IsCheckingTable.

But as SLaks commented, using Tasks available in newer versions of .NET would be better suited to what you're trying to do. Unfortunately, I'm not very familiar with them yet.

Community
  • 1
  • 1
Jeff B
  • 8,572
  • 17
  • 61
  • 140