Re: Help - Timing Logic
- From: "Chris Dunaway" <dunawayc@xxxxxxxxx>
- Date: 28 Nov 2006 13:47:54 -0800
Jay wrote:
I have a multi threaded VB.NET application (4 threads) that I use to
send text messages to many, many employees via system.timer at a 5
second interval. Basically, I look in a SQL table (queue) to
determine who needs to receive the text message then send the
message to the address. Only problem is, the employee may receive up
to 4 of the same messages because each thread gets the recors then
sends the message. I need somehow to prevent this... just can't
think of how. Somehow I need the other threads to know that another
thread is already using that record and move on to the next record.
I thought of getting the record then marking it (column value) as in
use and writing the code to look for records only where not in
use... problem is all 4 run fast enough to all use/mark the row.
Any thoughts?
Why not have just one thread that reads the records from the db and
creates "TextMessage" objects that are inserted into a synchronized
queue. Then each of your four sending threads can all get their values
from this queue. Since the queue is synchronized, none of the 4
threads will be able to get the same TextMessage object from the queue.
The TextMessage object I am referring to is one you would create
yourself to encapsulate the data from a row in your queue table. The
Queue class in System.Collections (or the generic one in
System.Collections.Generic) have a SyncRoot property that can be used
to lock the queue when a thread is retrieving an object from it.
The method might look like this (theQueue is defined elsewhere):
Public Function GetMessage() As TextMessage
Dim obj As TextMessage
'SyncLock prevents other threads from dequeing the same object.
SyncLock theQueue.SyncRoot
If theQueue.Count > 0 Then
obj = theQueue.Dequeue()
End If
End SyncLock
Return obj
End Function
Hope this helps,
Chris
.
- References:
- Help - Timing Logic
- From: Jay
- Re: Help - Timing Logic
- From: jeff
- Re: Help - Timing Logic
- From: Jay
- Re: Help - Timing Logic
- From: jeff
- Re: Help - Timing Logic
- From: Jay
- Re: Help - Timing Logic
- From: jeff
- Re: Help - Timing Logic
- From: Jay
- Re: Help - Timing Logic
- From: jeff
- Re: Help - Timing Logic
- From: Jay
- Help - Timing Logic
- Prev by Date: Re: Newbie Question here
- Next by Date: Re: set refresh rate for an active browser window
- Previous by thread: Re: Help - Timing Logic
- Next by thread: Re: Help - Timing Logic
- Index(es):
Relevant Pages
|