2014-12-03 45 views
0

timerTicks每秒都會觸發,並且運行子CheckTableForNewRecords只要IsCheckingTable = False如何通知調用子QueueUserWorkItem子已完成?

Sub CheckTableForNewRecords確定我的SQL表是否有新記錄,如果有,它將在新線程上運行子SQL.GetTrades

只有當螺紋接頭SQL.GetTrades完成後,我需要屬性IsCheckingTable更改爲False

我該怎麼做?

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 
+0

您應該使用'Task'來代替。 – SLaks 2014-12-03 21:07:50

+0

任務?對不起,不熟悉它。 – PtheGr8 2014-12-03 21:09:25

+0

http://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx – SLaks 2014-12-03 21:09:44

回答

0

假設你不關心線程安全什麼的,你能做到這一點會是這樣的一個很天真的方式:

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

你可能要標註IsCheckingTable波動的原因描述爲here。您可能還需要在QueueUserWorkItem內嘗試/趕上/終止以確保我們重置IsCheckingTable

但是正如SLaks所評論的,使用新版本的.NET中的任務可能更適合你想要做的事情。不幸的是,我還不是很熟悉它們。