473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help - Timing Logic

Jay
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?

Thanks a lot.

Jay

Nov 28 '06 #1
15 2578
Use a global property in the class or a module.

public IsRunning as boolean

Set to true if running, set to false if not.

You can still fire the threads, but have the processing pause will the
property is set to true.

Example

Private sub Do Work()
If IsRunning = true then
exit sub
else
Work
Nov 28 '06 #2
Jay
Thanks. I still need all 4 processes working to get a row from the table.
Only if one thread already has a row then the next 3 threads cant use the
same row.
"Ryan S. Thiele" <ma*****@verizo n.netwrote in message
news:GaYah.1409 6$LH2.13672@trn dny04...
Use a global property in the class or a module.

public IsRunning as boolean

Set to true if running, set to false if not.

You can still fire the threads, but have the processing pause will the
property is set to true.

Example

Private sub Do Work()
If IsRunning = true then
exit sub
else
Work
.
.
end if
end sub

Does this work?
--
--
Thiele Enterprises - The Power Is In Your Hands Now!
--
"Jay" <so*****@somewh ere.comwrote in message
news:O2******** ******@TK2MSFTN GP04.phx.gbl...
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?

Thanks a lot.

Jay

Nov 28 '06 #3

Question:

What is the reason for the 4 threads?

Do you need all these threads processing the same dataset?

Just asking, maybe you could change your approach to avoid this problem...

If not, investigate Record Locking / Transactions...

How are you retrieving your records in to memory for processing ...

ie,
- are you selecting on the entire contents of a 'message' table and looping
through an in-memory dataset...

- do you retrieve 'chunks' or rows from the message table based on
parameters...

You could use the internal locking mechanisms of the database to achieve
this ... but procede with caution...

Start Transaction...
SELECT * FROM MESSAGE TABLE
....
stuff this into a data table on the desktop
....
DELETE * FROM MESSAGE TABLE
....
punt / clean the table.
....
End Transaction.

The transactions places locks on the table ... but selecting the entire
tables contents ... you are essentially 'LOCKING' the entire table until the
transaction is over...

So, each thread's 'retrieve' process will have to wait in line until the
select and delete are processed. As well, anything triggering 'New Messages
to be Added' will be delayed until the Transaction is completed...

You may impact the overall performance of you application ... need to
investigate.

If locking does not work for you, you will need to implement some type of
logging / checking approach...

ie... have a log table...once a process has sent the message, write to a log
table ... message sent. Before each message is compiled and sent, check the
log table to see if another process has sent the message...if not, send you
message...
A snip of code would be great to figure out how you are retrieving a list of
messages and how you are processing each record...

Jeff.
"Jay" <so*****@somewh ere.comwrote in message
news:O2******** ******@TK2MSFTN GP04.phx.gbl...
>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?

Thanks a lot.

Jay

Nov 28 '06 #4
Jay
Thanks for the reply.

I do need all 4 threads running (maybe even more in the future). I can not
delete the row from the table... it needs to be there for later update use.
I am considering marking each row at insert (1 - 4) then having each of the
4 threads only select a row based on the mark. It's becoming a little
tricky... it would be great if I could lock a single row when selecting it.
"jeff" <jhersey at allnorth dottt comwrote in message
news:OP******** ******@TK2MSFTN GP03.phx.gbl...
>
Question:

What is the reason for the 4 threads?

Do you need all these threads processing the same dataset?

Just asking, maybe you could change your approach to avoid this problem...

If not, investigate Record Locking / Transactions...

How are you retrieving your records in to memory for processing ...

ie,
- are you selecting on the entire contents of a 'message' table and
looping through an in-memory dataset...

- do you retrieve 'chunks' or rows from the message table based on
parameters...

You could use the internal locking mechanisms of the database to achieve
this ... but procede with caution...

Start Transaction...
SELECT * FROM MESSAGE TABLE
...
stuff this into a data table on the desktop
...
DELETE * FROM MESSAGE TABLE
...
punt / clean the table.
...
End Transaction.

The transactions places locks on the table ... but selecting the entire
tables contents ... you are essentially 'LOCKING' the entire table until
the transaction is over...

So, each thread's 'retrieve' process will have to wait in line until the
select and delete are processed. As well, anything triggering 'New
Messages to be Added' will be delayed until the Transaction is
completed...

You may impact the overall performance of you application ... need to
investigate.

If locking does not work for you, you will need to implement some type of
logging / checking approach...

ie... have a log table...once a process has sent the message, write to a
log table ... message sent. Before each message is compiled and sent,
check the log table to see if another process has sent the message...if
not, send you message...
A snip of code would be great to figure out how you are retrieving a list
of messages and how you are processing each record...

Jeff.
"Jay" <so*****@somewh ere.comwrote in message
news:O2******** ******@TK2MSFTN GP04.phx.gbl...
>>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?

Thanks a lot.

Jay

Nov 28 '06 #5

are you selecting individual rows at a time from the database table ...

if so ... use a transaction ...

begin transaction ...

select a row from database ...

update the row in table set flag = 'Processed'

end transaction

this will lock the row until the end of transaction is issued ... as long as
the isolation level is set accordingly...

Again, please let us know how you are getting the information from the
database... then we can help!

If you are reading a bunch of rows in one statement, storing the rows in
in-memory datasets on the workstation, looping through the rows one by one
.... then you may need to either ...

implement as above locking only the records you retrieve / update - need to
watch out here for table locking ... may impact performance,
implement using an update flag at sent and 'check before send method'...
....
lots of options here, just need to know how you are retrieving your
data...and how you are processing the data.

Question, why do you need 4 threads running ... are they doing 'different
processes', sending different 'types' of messages ... sending the same
message ... just need 4 to make it faster ... what is the logic ...

Tieing a record to a thread my cause problems in the future ... what happens
when a thread stops or hangs ... those messages will not be processed...
When happens if somebody changes the ThreadID ... in the program ...

trying to help ..

Jeff

PS: you can lock individual rows ... look at how database transactions work
and incorporate it in you program ...


"Jay" <so*****@somewh ere.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
Thanks for the reply.

I do need all 4 threads running (maybe even more in the future). I can
not delete the row from the table... it needs to be there for later update
use. I am considering marking each row at insert (1 - 4) then having each
of the 4 threads only select a row based on the mark. It's becoming a
little tricky... it would be great if I could lock a single row when
selecting it.
"jeff" <jhersey at allnorth dottt comwrote in message
news:OP******** ******@TK2MSFTN GP03.phx.gbl...
>>
Question:

What is the reason for the 4 threads?

Do you need all these threads processing the same dataset?

Just asking, maybe you could change your approach to avoid this
problem...

If not, investigate Record Locking / Transactions...

How are you retrieving your records in to memory for processing ...

ie,
- are you selecting on the entire contents of a 'message' table and
looping through an in-memory dataset...

- do you retrieve 'chunks' or rows from the message table based on
parameters.. .

You could use the internal locking mechanisms of the database to achieve
this ... but procede with caution...

Start Transaction...
SELECT * FROM MESSAGE TABLE
...
stuff this into a data table on the desktop
...
DELETE * FROM MESSAGE TABLE
...
punt / clean the table.
...
End Transaction.

The transactions places locks on the table ... but selecting the entire
tables contents ... you are essentially 'LOCKING' the entire table until
the transaction is over...

So, each thread's 'retrieve' process will have to wait in line until the
select and delete are processed. As well, anything triggering 'New
Messages to be Added' will be delayed until the Transaction is
completed...

You may impact the overall performance of you application ... need to
investigate.

If locking does not work for you, you will need to implement some type of
logging / checking approach...

ie... have a log table...once a process has sent the message, write to a
log table ... message sent. Before each message is compiled and sent,
check the log table to see if another process has sent the message...if
not, send you message...
A snip of code would be great to figure out how you are retrieving a list
of messages and how you are processing each record...

Jeff.
"Jay" <so*****@somewh ere.comwrote in message
news:O2******* *******@TK2MSFT NGP04.phx.gbl.. .
>>>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?

Thanks a lot.

Jay


Nov 28 '06 #6
Jay
Thanks. I do need to select an individual row at a time and all 4 threads
need to do this. What trasaction isolation level would you recommend?
Perhaps a stored proc may be faster to execute and return the values as
opposed to building the transaction in the code. What has to happen is
every 5 seconds, and for each thread, a sub runs to get a single row then
send the message to dtr("numbertose ndto"). Because this app heavily relies
on timing it is important that all threads run and only one distinct row can
be returned at a time for each thread.

"jeff" <jhersey at allnorth dottt comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>
are you selecting individual rows at a time from the database table ...

if so ... use a transaction ...

begin transaction ...

select a row from database ...

update the row in table set flag = 'Processed'

end transaction

this will lock the row until the end of transaction is issued ... as long
as the isolation level is set accordingly...

Again, please let us know how you are getting the information from the
database... then we can help!

If you are reading a bunch of rows in one statement, storing the rows in
in-memory datasets on the workstation, looping through the rows one by one
... then you may need to either ...

implement as above locking only the records you retrieve / update - need
to watch out here for table locking ... may impact performance,
implement using an update flag at sent and 'check before send method'...
...
lots of options here, just need to know how you are retrieving your
data...and how you are processing the data.

Question, why do you need 4 threads running ... are they doing 'different
processes', sending different 'types' of messages ... sending the same
message ... just need 4 to make it faster ... what is the logic ...

Tieing a record to a thread my cause problems in the future ... what
happens when a thread stops or hangs ... those messages will not be
processed... When happens if somebody changes the ThreadID ... in the
program ...

trying to help ..

Jeff

PS: you can lock individual rows ... look at how database transactions
work and incorporate it in you program ...


"Jay" <so*****@somewh ere.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
>Thanks for the reply.

I do need all 4 threads running (maybe even more in the future). I can
not delete the row from the table... it needs to be there for later
update use. I am considering marking each row at insert (1 - 4) then
having each of the 4 threads only select a row based on the mark. It's
becoming a little tricky... it would be great if I could lock a single
row when selecting it.
"jeff" <jhersey at allnorth dottt comwrote in message
news:OP******* *******@TK2MSFT NGP03.phx.gbl.. .
>>>
Question:

What is the reason for the 4 threads?

Do you need all these threads processing the same dataset?

Just asking, maybe you could change your approach to avoid this
problem...

If not, investigate Record Locking / Transactions...

How are you retrieving your records in to memory for processing ...

ie,
- are you selecting on the entire contents of a 'message' table and
looping through an in-memory dataset...

- do you retrieve 'chunks' or rows from the message table based on
parameters. ..

You could use the internal locking mechanisms of the database to achieve
this ... but procede with caution...

Start Transaction...
SELECT * FROM MESSAGE TABLE
...
stuff this into a data table on the desktop
...
DELETE * FROM MESSAGE TABLE
...
punt / clean the table.
...
End Transaction.

The transactions places locks on the table ... but selecting the entire
tables contents ... you are essentially 'LOCKING' the entire table until
the transaction is over...

So, each thread's 'retrieve' process will have to wait in line until the
select and delete are processed. As well, anything triggering 'New
Messages to be Added' will be delayed until the Transaction is
completed.. .

You may impact the overall performance of you application ... need to
investigate .

If locking does not work for you, you will need to implement some type
of logging / checking approach...

ie... have a log table...once a process has sent the message, write to a
log table ... message sent. Before each message is compiled and sent,
check the log table to see if another process has sent the message...if
not, send you message...
A snip of code would be great to figure out how you are retrieving a
list of messages and how you are processing each record...

Jeff.
"Jay" <so*****@somewh ere.comwrote in message
news:O2****** ********@TK2MSF TNGP04.phx.gbl. ..
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?

Thanks a lot.

Jay

Nov 28 '06 #7

Still do not know why you need 4 threads ? Speed ? Does runnig the process
in one thread not work fast enough for you since timing is import ? Why 4
threads why not 6,12 , 3, 36 ... ? How do you determine which row a
'thread retrieves' ??? does it simply select the next available row from the
DATABASE ??? How does the thread determine 'the next row'???

If mutli-threading is duplicating 'send messages' it is not working
properly...
If you are multi-threading to improve performance ... maybe look at the
design...
If you need to implement a locking mechanism / or / logging mechanism / or /
a checking mechanism to avoid duplicate messages caused by multi-threading
.... these will all come at a cost ... performance cost!

What is bottle necking your process that you need 4 threads? Is it the READ
from the database ... is it the SENDING the text message? Is it connecting
to the database ??? do not know here ...

Look at what is causing problem ...

- you need better performance ... so, multiple thread it! However,
mutli-threading the entire process is causing issues ... duplicate records
.... now you either need to incorporate a locking procedure ... or a checking
procedure to avoid duplicate messages... all this has a cost to the overall
performance ... adding more threads may in fact negatively impact the
overall performance....

Maybe implement a message broker ...

- message broker gets all the necessary messages or message id's from the
database to be sent ... in-memory list...
- message broker loops through list of messageIDs ...
- message broker starts another thread for processing the sendtextmessage
functionality for each messageID...
- message broker will include the messageID so the process knows which
message to get and process...
- message broker will ensure each database row is only processed once...

It is very hard to help you without knowing exactly what you are doing...

Again, I will ask, what rational / reasoning did you use for using 4 threads
? Performance ? Speed ? ... Where is the bottle neck in the process that
requires you to multi-thread it? Maybe just move the 'bottle neck section'
to another thread ... ?

Grasping at straws ...

Code would be nice how to see what you are doing ...

Jeff





"Jay" <so*****@somewh ere.comwrote in message
news:uB******** ******@TK2MSFTN GP04.phx.gbl...
Thanks. I do need to select an individual row at a time and all 4 threads
need to do this. What trasaction isolation level would you recommend?
Perhaps a stored proc may be faster to execute and return the values as
opposed to building the transaction in the code. What has to happen is
every 5 seconds, and for each thread, a sub runs to get a single row then
send the message to dtr("numbertose ndto"). Because this app heavily
relies on timing it is important that all threads run and only one
distinct row can be returned at a time for each thread.

"jeff" <jhersey at allnorth dottt comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>>
are you selecting individual rows at a time from the database table ...

if so ... use a transaction ...

begin transaction ...

select a row from database ...

update the row in table set flag = 'Processed'

end transaction

this will lock the row until the end of transaction is issued ... as long
as the isolation level is set accordingly...

Again, please let us know how you are getting the information from the
database... then we can help!

If you are reading a bunch of rows in one statement, storing the rows in
in-memory datasets on the workstation, looping through the rows one by
one ... then you may need to either ...

implement as above locking only the records you retrieve / update - need
to watch out here for table locking ... may impact performance,
implement using an update flag at sent and 'check before send method'...
...
lots of options here, just need to know how you are retrieving your
data...and how you are processing the data.

Question, why do you need 4 threads running ... are they doing 'different
processes', sending different 'types' of messages ... sending the same
message ... just need 4 to make it faster ... what is the logic ...

Tieing a record to a thread my cause problems in the future ... what
happens when a thread stops or hangs ... those messages will not be
processed... When happens if somebody changes the ThreadID ... in the
program ...

trying to help ..

Jeff

PS: you can lock individual rows ... look at how database transactions
work and incorporate it in you program ...


"Jay" <so*****@somewh ere.comwrote in message
news:%2******* *********@TK2MS FTNGP02.phx.gbl ...
>>Thanks for the reply.

I do need all 4 threads running (maybe even more in the future). I can
not delete the row from the table... it needs to be there for later
update use. I am considering marking each row at insert (1 - 4) then
having each of the 4 threads only select a row based on the mark. It's
becoming a little tricky... it would be great if I could lock a single
row when selecting it.
"jeff" <jhersey at allnorth dottt comwrote in message
news:OP****** ********@TK2MSF TNGP03.phx.gbl. ..

Question:

What is the reason for the 4 threads?

Do you need all these threads processing the same dataset?

Just asking, maybe you could change your approach to avoid this
problem...

If not, investigate Record Locking / Transactions...

How are you retrieving your records in to memory for processing ...

ie,
- are you selecting on the entire contents of a 'message' table and
looping through an in-memory dataset...

- do you retrieve 'chunks' or rows from the message table based on
parameters.. .

You could use the internal locking mechanisms of the database to
achieve this ... but procede with caution...

Start Transaction...
SELECT * FROM MESSAGE TABLE
...
stuff this into a data table on the desktop
...
DELETE * FROM MESSAGE TABLE
...
punt / clean the table.
...
End Transaction.

The transactions places locks on the table ... but selecting the entire
tables contents ... you are essentially 'LOCKING' the entire table
until the transaction is over...

So, each thread's 'retrieve' process will have to wait in line until
the select and delete are processed. As well, anything triggering 'New
Messages to be Added' will be delayed until the Transaction is
completed. ..

You may impact the overall performance of you application ... need to
investigat e.

If locking does not work for you, you will need to implement some type
of logging / checking approach...

ie... have a log table...once a process has sent the message, write to
a log table ... message sent. Before each message is compiled and
sent, check the log table to see if another process has sent the
message... if not, send you message...
A snip of code would be great to figure out how you are retrieving a
list of messages and how you are processing each record...

Jeff.
"Jay" <so*****@somewh ere.comwrote in message
news:O2***** *********@TK2MS FTNGP04.phx.gbl ...
>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?
>
Thanks a lot.
>
Jay
>


Nov 28 '06 #8
Jay
It is extremely important for the employees to receive the messages almost
immediately after identifying (time val in table). So, threading ensures I
can execute multiple processes at the same time... only problem is these
threads read from the same queue table and have the potential to send the
same message to the same guy x number of times. where x is the number of
threads running.

"jeff" <jhersey at allnorth dottt comwrote in message
news:uQ******** ********@TK2MSF TNGP06.phx.gbl. ..
>
Still do not know why you need 4 threads ? Speed ? Does runnig the
process in one thread not work fast enough for you since timing is import
? Why 4 threads why not 6,12 , 3, 36 ... ? How do you determine which
row a 'thread retrieves' ??? does it simply select the next available row
from the DATABASE ??? How does the thread determine 'the next row'???

If mutli-threading is duplicating 'send messages' it is not working
properly...
If you are multi-threading to improve performance ... maybe look at the
design...
If you need to implement a locking mechanism / or / logging mechanism / or
/ a checking mechanism to avoid duplicate messages caused by
multi-threading ... these will all come at a cost ... performance cost!

What is bottle necking your process that you need 4 threads? Is it the
READ from the database ... is it the SENDING the text message? Is it
connecting to the database ??? do not know here ...

Look at what is causing problem ...

- you need better performance ... so, multiple thread it! However,
mutli-threading the entire process is causing issues ... duplicate records
... now you either need to incorporate a locking procedure ... or a
checking procedure to avoid duplicate messages... all this has a cost to
the overall performance ... adding more threads may in fact negatively
impact the overall performance....

Maybe implement a message broker ...

- message broker gets all the necessary messages or message id's from the
database to be sent ... in-memory list...
- message broker loops through list of messageIDs ...
- message broker starts another thread for processing the sendtextmessage
functionality for each messageID...
- message broker will include the messageID so the process knows which
message to get and process...
- message broker will ensure each database row is only processed once...

It is very hard to help you without knowing exactly what you are doing...

Again, I will ask, what rational / reasoning did you use for using 4
threads ? Performance ? Speed ? ... Where is the bottle neck in the
process that requires you to multi-thread it? Maybe just move the 'bottle
neck section' to another thread ... ?

Grasping at straws ...

Code would be nice how to see what you are doing ...

Jeff





"Jay" <so*****@somewh ere.comwrote in message
news:uB******** ******@TK2MSFTN GP04.phx.gbl...
>Thanks. I do need to select an individual row at a time and all 4
threads need to do this. What trasaction isolation level would you
recommend? Perhaps a stored proc may be faster to execute and return the
values as opposed to building the transaction in the code. What has to
happen is every 5 seconds, and for each thread, a sub runs to get a
single row then send the message to dtr("numbertose ndto"). Because this
app heavily relies on timing it is important that all threads run and
only one distinct row can be returned at a time for each thread.

"jeff" <jhersey at allnorth dottt comwrote in message
news:%2******* *********@TK2MS FTNGP03.phx.gbl ...
>>>
are you selecting individual rows at a time from the database table ...

if so ... use a transaction ...

begin transaction ...

select a row from database ...

update the row in table set flag = 'Processed'

end transaction

this will lock the row until the end of transaction is issued ... as
long as the isolation level is set accordingly...

Again, please let us know how you are getting the information from the
database... then we can help!

If you are reading a bunch of rows in one statement, storing the rows in
in-memory datasets on the workstation, looping through the rows one by
one ... then you may need to either ...

implement as above locking only the records you retrieve / update - need
to watch out here for table locking ... may impact performance,
implement using an update flag at sent and 'check before send method'...
...
lots of options here, just need to know how you are retrieving your
data...and how you are processing the data.

Question, why do you need 4 threads running ... are they doing
'different processes', sending different 'types' of messages ... sending
the same message ... just need 4 to make it faster ... what is the logic
...

Tieing a record to a thread my cause problems in the future ... what
happens when a thread stops or hangs ... those messages will not be
processed.. . When happens if somebody changes the ThreadID ... in the
program ...

trying to help ..

Jeff

PS: you can lock individual rows ... look at how database transactions
work and incorporate it in you program ...


"Jay" <so*****@somewh ere.comwrote in message
news:%2****** **********@TK2M SFTNGP02.phx.gb l...
Thanks for the reply.

I do need all 4 threads running (maybe even more in the future). I can
not delete the row from the table... it needs to be there for later
update use. I am considering marking each row at insert (1 - 4) then
having each of the 4 threads only select a row based on the mark. It's
becoming a little tricky... it would be great if I could lock a single
row when selecting it.
"jeff" <jhersey at allnorth dottt comwrote in message
news:OP***** *********@TK2MS FTNGP03.phx.gbl ...
>
Question:
>
What is the reason for the 4 threads?
>
Do you need all these threads processing the same dataset?
>
Just asking, maybe you could change your approach to avoid this
problem.. .
>
If not, investigate Record Locking / Transactions...
>
How are you retrieving your records in to memory for processing ...
>
ie,
- are you selecting on the entire contents of a 'message' table and
looping through an in-memory dataset...
>
- do you retrieve 'chunks' or rows from the message table based on
parameters. ..
>
You could use the internal locking mechanisms of the database to
achieve this ... but procede with caution...
>
Start Transaction...
SELECT * FROM MESSAGE TABLE
...
stuff this into a data table on the desktop
...
DELETE * FROM MESSAGE TABLE
...
punt / clean the table.
...
End Transaction.
>
The transactions places locks on the table ... but selecting the
entire tables contents ... you are essentially 'LOCKING' the entire
table until the transaction is over...
>
So, each thread's 'retrieve' process will have to wait in line until
the select and delete are processed. As well, anything triggering
'New Messages to be Added' will be delayed until the Transaction is
completed.. .
>
You may impact the overall performance of you application ... need to
investigate .
>
If locking does not work for you, you will need to implement some type
of logging / checking approach...
>
ie... have a log table...once a process has sent the message, write to
a log table ... message sent. Before each message is compiled and
sent, check the log table to see if another process has sent the
message...i f not, send you message...
>
>
A snip of code would be great to figure out how you are retrieving a
list of messages and how you are processing each record...
>
Jeff.
>
>
"Jay" <so*****@somewh ere.comwrote in message
news:O2**** **********@TK2M SFTNGP04.phx.gb l...
>>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
>>determi ne 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?
>>
>Thanks a lot.
>>
>Jay
>>
>
>

Nov 28 '06 #9

I know it is a time critical system ... employees need the information
ASAP... but what I do not understand is ...

- where is the application running ... on a server ... on a users work
station?
- who will be responsible for executing / sending messages ... one computer
or many different computers?
- will this message sender reside on a server / database server / or a
user's workstation ... ???
- what database are you using ???
- is this functionality wrapped up in a large application?
- why 4 threads ?
- how does you program determine which message to send ... parameter list
please.

------------------------------------
If this information is so mission critical, and employees must get their
message ASAP ... put a trigger on the database table.

.... wrap the necessary functionality is a small stand alone exe application
....
.... build your application so it receives commandline parameters / string
....
.... build a trigger on the database .. fire on inserts ... have it call you
message program with a command parameter (messageID) ...
.... install you application on the database server .... this will speed up
the connection / retrievals and so on ... no network latency.

Each time a message is inserted in the table, the trigger fires, calling
your EXE with the appropriate parameter string, exe starts, fires off
message ... done. Small exe can run as many times on the server...

------------------------------------

If you are using a timestamp for determining which messages to send ...
incorporate another table ... LastTimeExecute d ...

table: MessengerExecut ion
field: LastDateTimeFir ed

begin transactions ... lock table.
select lastdatetimefir ed from this table...in a variable
update field with thread date/time ...
end transaction.
return the select value and the update value

select messages where date is between lastdatetimefir ed and the
updateddatetime I just used...

------------------------------------

Again, there are many solutions ... just do not completely understand what
you are doing...

------------------------------------
Bottom line ...

- you have determined that you need to run a mutli-threaded process for
this.

- so, in order to avoid DUPLICATE MESSAGES .... you either have to
employee...

Transaction and Database Locking - look at isolation levels / settings

or

A booker type of system...

------------------------------------
have the broker continually pool the table ... this will only work it 1
machine is designated for messaging! If more than one machine will be used
for messaging ... you will have to roll up your sleeves and look at
Transactions and Isolation Levels.

CheckMessage

Do Until Company.Revenue s < 0
MessegeID = broker.getNextM essage()
broker.sendMess age(MessageID)
Loop

SendMessage(Mes sageID)

Create a new thread...
SendMessage(Mes sageID) in this new thread...
Return to the MessageBroker.C heckMessage...w hile the other thread is
preparing and sending the message...

This will continuious poll the database server for new messages! ...

------------------------------------

However, if it is mission criticial users receive this information ASAP ...
TRIGGER on database table! If the user can wait 10 seconds .. build a
BROKER ... and have it spawn as many threads needed to send the messages in
the QUEUE ... have it control the process...and sending!

------------------------------------

Again, many solutions, depends on your needs ... code sample, table
structure ... some thing to trying and figure out how exactly your 'threads'
are getting 1 message...

Jeff.
"Jay" <so*****@somewh ere.comwrote in message
news:OL******** *****@TK2MSFTNG P02.phx.gbl...
It is extremely important for the employees to receive the messages almost
immediately after identifying (time val in table). So, threading ensures
I can execute multiple processes at the same time... only problem is these
threads read from the same queue table and have the potential to send the
same message to the same guy x number of times. where x is the number of
threads running.

"jeff" <jhersey at allnorth dottt comwrote in message
news:uQ******** ********@TK2MSF TNGP06.phx.gbl. ..
>>
Still do not know why you need 4 threads ? Speed ? Does runnig the
process in one thread not work fast enough for you since timing is import
? Why 4 threads why not 6,12 , 3, 36 ... ? How do you determine which
row a 'thread retrieves' ??? does it simply select the next available row
from the DATABASE ??? How does the thread determine 'the next row'???

If mutli-threading is duplicating 'send messages' it is not working
properly...
If you are multi-threading to improve performance ... maybe look at the
design...
If you need to implement a locking mechanism / or / logging mechanism /
or / a checking mechanism to avoid duplicate messages caused by
multi-threading ... these will all come at a cost ... performance cost!

What is bottle necking your process that you need 4 threads? Is it the
READ from the database ... is it the SENDING the text message? Is it
connecting to the database ??? do not know here ...

Look at what is causing problem ...

- you need better performance ... so, multiple thread it! However,
mutli-threading the entire process is causing issues ... duplicate
records ... now you either need to incorporate a locking procedure ... or
a checking procedure to avoid duplicate messages... all this has a cost
to the overall performance ... adding more threads may in fact negatively
impact the overall performance....

Maybe implement a message broker ...

- message broker gets all the necessary messages or message id's from the
database to be sent ... in-memory list...
- message broker loops through list of messageIDs ...
- message broker starts another thread for processing the sendtextmessage
functionalit y for each messageID...
- message broker will include the messageID so the process knows which
message to get and process...
- message broker will ensure each database row is only processed once...

It is very hard to help you without knowing exactly what you are doing...

Again, I will ask, what rational / reasoning did you use for using 4
threads ? Performance ? Speed ? ... Where is the bottle neck in the
process that requires you to multi-thread it? Maybe just move the
'bottle neck section' to another thread ... ?

Grasping at straws ...

Code would be nice how to see what you are doing ...

Jeff





"Jay" <so*****@somewh ere.comwrote in message
news:uB******* *******@TK2MSFT NGP04.phx.gbl.. .
>>Thanks. I do need to select an individual row at a time and all 4
threads need to do this. What trasaction isolation level would you
recommend? Perhaps a stored proc may be faster to execute and return the
values as opposed to building the transaction in the code. What has to
happen is every 5 seconds, and for each thread, a sub runs to get a
single row then send the message to dtr("numbertose ndto"). Because this
app heavily relies on timing it is important that all threads run and
only one distinct row can be returned at a time for each thread.

"jeff" <jhersey at allnorth dottt comwrote in message
news:%2****** **********@TK2M SFTNGP03.phx.gb l...

are you selecting individual rows at a time from the database table ...

if so ... use a transaction ...

begin transaction ...

select a row from database ...

update the row in table set flag = 'Processed'

end transaction

this will lock the row until the end of transaction is issued ... as
long as the isolation level is set accordingly...

Again, please let us know how you are getting the information from the
database.. . then we can help!

If you are reading a bunch of rows in one statement, storing the rows
in in-memory datasets on the workstation, looping through the rows one
by one ... then you may need to either ...

implement as above locking only the records you retrieve / update -
need to watch out here for table locking ... may impact performance,
implement using an update flag at sent and 'check before send
method'...
...
lots of options here, just need to know how you are retrieving your
data...and how you are processing the data.

Question, why do you need 4 threads running ... are they doing
'different processes', sending different 'types' of messages ...
sending the same message ... just need 4 to make it faster ... what is
the logic ...

Tieing a record to a thread my cause problems in the future ... what
happens when a thread stops or hangs ... those messages will not be
processed. .. When happens if somebody changes the ThreadID ... in the
program ...

trying to help ..

Jeff

PS: you can lock individual rows ... look at how database transactions
work and incorporate it in you program ...


"Jay" <so*****@somewh ere.comwrote in message
news:%2***** ***********@TK2 MSFTNGP02.phx.g bl...
Thanks for the reply.
>
I do need all 4 threads running (maybe even more in the future). I
can not delete the row from the table... it needs to be there for
later update use. I am considering marking each row at insert (1 - 4)
then having each of the 4 threads only select a row based on the mark.
It's becoming a little tricky... it would be great if I could lock a
single row when selecting it.
>
>
"jeff" <jhersey at allnorth dottt comwrote in message
news:OP**** **********@TK2M SFTNGP03.phx.gb l...
>>
>Question :
>>
>What is the reason for the 4 threads?
>>
>Do you need all these threads processing the same dataset?
>>
>Just asking, maybe you could change your approach to avoid this
>problem. ..
>>
>If not, investigate Record Locking / Transactions...
>>
>How are you retrieving your records in to memory for processing ...
>>
>ie,
>- are you selecting on the entire contents of a 'message' table and
>looping through an in-memory dataset...
>>
>- do you retrieve 'chunks' or rows from the message table based on
>parameters ...
>>
>You could use the internal locking mechanisms of the database to
>achieve this ... but procede with caution...
>>
>Start Transaction...
>SELECT * FROM MESSAGE TABLE
>...
>stuff this into a data table on the desktop
>...
>DELETE * FROM MESSAGE TABLE
>...
>punt / clean the table.
>...
>End Transaction.
>>
>The transactions places locks on the table ... but selecting the
>entire tables contents ... you are essentially 'LOCKING' the entire
>table until the transaction is over...
>>
>So, each thread's 'retrieve' process will have to wait in line until
>the select and delete are processed. As well, anything triggering
>'New Messages to be Added' will be delayed until the Transaction is
>completed. ..
>>
>You may impact the overall performance of you application ... need to
>investigat e.
>>
>If locking does not work for you, you will need to implement some
>type of logging / checking approach...
>>
>ie... have a log table...once a process has sent the message, write
>to a log table ... message sent. Before each message is compiled and
>sent, check the log table to see if another process has sent the
>message... if not, send you message...
>>
>>
>A snip of code would be great to figure out how you are retrieving a
>list of messages and how you are processing each record...
>>
>Jeff.
>>
>>
>"Jay" <so*****@somewh ere.comwrote in message
>news:O2*** ***********@TK2 MSFTNGP04.phx.g bl...
>>>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
>>>determin e 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
>>>messag e. I need somehow to prevent this... just can't think of how.
>>>Someho w I need the other threads to know that another thread is
>>>alread y 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
>>>writin g 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?
>>>
>>Thanks a lot.
>>>
>>Jay
>>>
>>
>>
>


Nov 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3144
by: rhmd | last post by:
Just found Python and I love it. What an elegant language! I would like to use it for various applications, but the mathematical calculations are way too slow (a million sines 8 seconds in Python vs. 2 seconds in Excel VBA), so was thinking of learning enough C++ to do the number crunching in C++ and integrating the C++ routines with Python. My question: My question: Which compiler works best with Python? I use Windows XP and 98. Have...
6
1822
by: S. David Rose | last post by:
Hello All! I am new to Python, and wanted to know if I might ask you a question regarding timing. I want a main loop which takes photos from 8 different web-cams, each can be addressed by http://ip-addr/image.jpg. That's the easy part, but I want to have 2 cameras poll 3-times a second, 4 cameras poll 2 times a second, and the remaining 2 cameras poll once a second. I have found lots of info suggesting the use of threads for this, all...
7
2265
by: ZRexRider | last post by:
Hi, I have trigger that enforces the creation of a sortorder that is always 1 digit higher than the current highest on Inserts. This trigger works great if I add one row at a time so I think the logic is sound. However, I have a Stored Procedure that copies a bunch of rows into this table and all of the SortOrder values come up as 0. This stored procedure is doing an "Insert Into" and will insert numerous rows (10-20) at once.
9
2229
by: Lil | last post by:
Hi Everyone! I've been trying to figure out this weird bug in my program. I have a python program that calls a C function that reads in a binary file into a buffer. In the C program, buffer is allocated by calling malloc. The C program runs perfectly fine but when I use python to call the C function, it core dumps at malloc. I've tried multiple binary files with different sizes and the result is: if file size is < 20 bytes , works fine...
31
2637
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant but it appears to work. using time, i measured it takes .041s to execute, which i admit isnt much. but, the problem is that this procedure will be called many, many times in my project (probably at least a few thousand times, if not more) so...
9
1753
by: Amir Ghezelbash | last post by:
Hey every body i had a question i am in process of writing an application, where this application needs to check the database on hourly bases to see if they are any information that are needed to be processed in the next upcoming hour so my application has to to connect to data base right on (for example) 5:00:00 then check all the jobs that are due on 5:00:00 5:15:00 5:30:00
1
991
by: le9569 | last post by:
Hello all, I have a dropdownlist with more than 40 items inside. I am trying to write a function that if the user change more than 20 items in the dropdownlist within 4 seconds then I redirect to another page. This action will be using in the SelectedIndexChanged. The problem is how do I start timing?
1
1420
by: Novice | last post by:
Hi all, I'm at my wit's end on trying to insert some timing code into the server side code that parses the hashed data contained in the hidden field being submitted to the server I've tried placing timing code in all of the following: Page_Load OnInit LoadViewState(object savedState) object SaveViewState()
3
1696
by: gregory_may | last post by:
I have an application where I am using a System Thread to capture the screen & Broadcast it to clients. Its "working", but the timing on the background thread gets wildly erratic at times. Some times, its right away, some times after 10 seconds. I have included the setup of the process and the outline of the Call Back Method. As posted, the callback method can be mildly erratic (only doing a debug.writeline) I am guessing up to an 80%...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.