469,575 Members | 1,221 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Using Thread.Join()

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
Nov 18 '05 #1
2 2027
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.

--
Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits
"Elizabeth Harmon" <EH*****@bloomingtonfarms.com> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl... 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

Nov 18 '05 #2
Thanks for the Advice, i will go with a select statement in the loop so i
can test for those conditions
Samantha
"Alvin Bruney" <va******@hotspammailme.com> wrote in message
news:ud**************@TK2MSFTNGP10.phx.gbl...
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.

--
Regards,
Alvin Bruney
Got Tidbits? Get it here
www.networkip.net/tidbits
"Elizabeth Harmon" <EH*****@bloomingtonfarms.com> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl...
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


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Peter Hansen | last post: by
13 posts views Thread by Paul | last post: by
4 posts views Thread by denton | last post: by
1 post views Thread by Elizabeth Harmon | last post: by
14 posts views Thread by Joe | last post: by
6 posts views Thread by Extremest | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.