Using Thread.Join()

  • Thread starter Elizabeth Harmon
  • Start date
E

Elizabeth Harmon

Hi All

I am just double checking myself here.

I have two threads that i am running in an application, One Thread Updates a
client side Table in a local DB, another Updates a Server Side Table on a
Server DB (I know i dont like it either but for some reason they want two
seperate DB's, go figure!!)
Anyway, I use the following code to Kick off the Threads:


'While the thread state is running, loop until it is
stopped
While intRunningState <> connThread.ThreadState.Stopped
Or intRunningState2 <> connUpldThread.ThreadState.Stopped

If Not blnIsRunning Then
'Writer Updates the Screen for the User
WriteStatusUpdate(writer, "<BR><FONT
color='blue'>Retrieving Records, Please Wait....</FONT>", True)
blnIsRunning = True
connThread.Start()
intRunningState = connThread.ThreadState
connThread.Join()
connUpldThread.Start()
connUpldThread.Join()
intRunningState2 = connUpldThread.ThreadState

End If
intRunningState = connThread.ThreadState
intRunningState2 = connUpldThread.ThreadState
If connUpldThread.ThreadState = 0 Then
'Writer Updates the Screen for the User
WriteStatusUpdate(writer, "<BR><FONT
color='blue'>Sending Records, Please Wait....</FONT>", True)
End If
End While

What i want to know is, am i using the Join statement Correctly? I do want
one Thread to finish before the other starts in an effort to co-ordinate
this process. Please let me know

Thanks in advance

Samantha
 
A

Alvin Bruney

The join is correct for what you are attempting to do.

I've taken the liberty of glancing over your code.
'While the thread state is running, loop until it is stopped
logic error here. what if your thread can't talk to the database, thru
connectivity issues, OR the thread aborts or is suspended. What if the
thread fails to stop or you cannot read the thread state? Your application
enters an infinite loop. The loop is tight enough to bring down the server.
You should OR the different thread states together to test for the other
cases like aborted | suspended | stopped etc. instead of just testing for
stopped state

There is still another issue. You aren't protecting your code against a long
running thread. What if your thread decides to run for 5 hours? You have no
safeguard in the code for that. For this you may want to force an abort
after an unnecessarily long period of time.
 
E

Elizabeth Harmon

Thanks for the Advice, i will go with a select statement in the loop so i
can test for those conditions
Samantha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top