Queue is not empty but Workerthread doesn't know that.
Hi, this is in Visual Basic no C#, I couldn't find a good VB.NET Forum..maybe I should change the language :-) Anyway, I hope somebody can help me.
I am writing an Application which should watch movements of shares on an Exchange of Stuttgart.
I have a DataStream-Object in VB.NET which provides my app continuously with new prices of shares. This happens via an Eventhandler which delivers Security-Number, time and new price of the share.I also wrote a class for Share-Objects with some properties but this is not really important for the problem.
The way my app should handle the new prices should look like this: When an Event for a new price for a share is coming in, the Share-Object should be picked up from a hashtable, the new price should be stored as a property in the Share-Obj., and this Object should be put in a queue.
The Workerthread should wait if an element is in the queue, and if yes, it should do some code with the new price, then return again to waiting state.
So far, so good, but here comes the PROBLEM: Testing my app under real conditions (10.000 Shares) revealed that it seems like some Quotations (new Prices) were not treated in the Workerthread. I could compare that by looking on the historic list of prices.
First I thought the new prices were not stored in the Share-Object, but testing with only one Share Object and 4 new Prices revealed that the new Prices WERE stored and the Share was put in the queue, BUT the Workerthread never recognized that there is something in the queue and did NOTHING. So the new Prices were overwritten with the newer ones until the Sub, where the Workerthread was started from and the test-events were send to the Event-Handling Code, finished. After that the Workerthread began its work, realized that there were 3 elements in the queue, but all were the same Share-Object with the same property: The last Price, and not all the Prices in the meantime.
Now here is the Code which starts all. “Wertpapier” means Share and refers to the Share-Object. “Wp_list” is a hashtable which stores the Share-Objects:
###################
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim thre As New System.Threading.Thread(AddressOf CheckBrief)
thre.Start()
Dim thre2 As New System.Threading.Thread(AddressOf CheckGeld)
thre2.Start()
Dim wertp As New Wertpapier(950887)
wp_list.Add(950887, wertp)
Dim x As Integer = 27
Dim i As Integer
give_handler(950887, 0.25, CDate("19:25:10"))
give_handler_geld(950887, 0.23, CDate("19:25:10"))
For i = 0 To 10000000
x = Math.Sqrt(x)
Next
give_handler(950887, 0.24, CDate("19:25:31"))
give_handler_geld(950887, 0.22, CDate("19:25:31"))
For i = 0 To 10000000
x = Math.Sqrt(x)
Next
give_handler(950887, 0.23, CDate("19:26:36"))
give_handler_geld(950887, 0.21, CDate("19:26:36"))
For i = 0 To 10000000
x = Math.Sqrt(x)
Next
give_handler(950887, 0.24, CDate("19:26:51"))
give_handler_geld(950887, 0.22, CDate("19:26:51"))
For i = 0 To 10000000
x = Math.Sqrt(x)
Next
give_handler(950887, 0.23, CDate("19:27:23"))
give_handler_geld(950887, 0.21, CDate("19:27:23"))
For i = 0 To test_array.Length - 1
Debug.Write(test_array(i))
Next
End Sub
###################
The for-next loop at the end returns what was done in the Workerthread, the test_array is filled there. While testing this code, everytime it returns only one row although it should be 5 because there are 5 events, means 5 new prices for the share. (Btw: “Zeit” means Time.)
The Test-Code for the Handler for the new Bids (“Geld” in German) and new Asks (“Brief”):
###################
Private Sub give_handler(ByVal SymbolNr_brief As Integer, ByVal Kurs_brief As Single, ByVal Zeit_brief As Date)
Dim wp_for_queue_brief As Wertpapier = CType(wp_list.Item(SymbolNr_brief), Wertpapier)
With wp_for_queue_brief
.set_brief_neu(Kurs_brief)
.set_zeit_neu(Zeit_brief)
End With
myqueue.Enqueue(wp_for_queue_brief)
'start_work.Set()
Label4.Text = System.Threading.Interlocked.Increment(counter_1)
End Sub
Private Sub give_handler_geld(ByVal symbolnr_geld As Integer, ByVal kurs_geld As Single, ByVal zeit_geld As Date)
Dim wp_for_queue As Wertpapier = CType(wp_list.Item(symbolnr_geld), Wertpapier)
With wp_for_queue
.set_geld_neu(kurs_geld)
End With
myqueue_geld.Enqueue(wp_for_queue)
start_work_geld.Set()
End Sub
###################
Finally, the Workerthreads for Geld and Brief:
###################
Public Sub CheckBrief()
‘some declarations go here…
Do
start_work.WaitOne(1, False) 'if a comment that out, nothing changes
If myqueue.Count > 0 Then
wp = CType(myqueue.Dequeue(), Wertpapier)
SyncLock wp
With wp
zeit_tai = .get_zeit_neu
brief_neu = .get_brief_neu
brief_alt = .get_brief
diff = brief_neu - brief_alt
wp_wkn = .get_wkn
lfndurchschnitt = .get_LaufDurchschnitt
.Increase_counts()
.Increase_prices(brief_neu)
wp_counts = .get_counts()
End With
End SyncLock
test_array(xi) = zeit_tai.ToString & ";" & brief_neu.ToString & ";" & brief_alt.ToString & ";" & diff.ToString & ";" & lfndurchschnitt.ToString & vbNewLine
xi += 1
End If
Loop Until thread_stop
End Sub
###################
Public Sub CheckGeld()
Dim wp As Wertpapier
Do
start_work_geld.WaitOne(10, False)
If myqueue_geld.Count > 0 Then
wp = CType(myqueue_geld.Dequeue(), Wertpapier)
SyncLock wp
wp.set_geld(wp.get_geld_neu)
End SyncLock
End If
Loop Until thread_abbrechen
End Sub
###################
I'm sitting over this for days....I would be glad if somebody could help me.