Re: Help - Timing Logic



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

.



Relevant Pages

  • Re: Program flow with multi-threading app - corrupted datatables
    ... The synclock on its own was enough to fix this problem - the majority ... of the updates to the datatable were value updates rather than row ... Let the threads fill (enqueue) the queue with datarows while synclocking it. ... At the end of my timer event I would refresh my datagridview. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Threading question....
    ... create a new class that inherits from the Queue class. ... Override the Enqueue and Dequeue methods and put a synclock ... > do care about the sequence of requests. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Waiting on a Thread - Revisited
    ... SyncLock construct because it served as a way of pacing outgoing messages. ... Messages have to be sent out synchronously, and the polling thread is ... capable of adding messages to the queue much faster than they can be sent. ... Before it reads from the queue, it gets a lock on it ...
    (microsoft.public.dotnet.general)
  • Re: How to improve performance of Queue accessing between 2 threads?
    ... SyncLock _queue.SyncRoot ... The other thread cannot access the data because you hold the lock on the ... I write program that sharing Queue between 2 threads which 1 thread add ... Here is some snippet code:- ...
    (microsoft.public.dotnet.framework)
  • Re: Waiting on a Thread - Revisited
    ... SyncLock construct because it served as a way of pacing outgoing messages. ... Messages have to be sent out synchronously, and the polling thread is ... capable of adding messages to the queue much faster than they can be sent. ... Before it reads from the queue, it gets a lock on it ...
    (microsoft.public.dotnet.languages.vb)