Using Thread.Join()

Discussion in 'ASP .Net' started by Elizabeth Harmon, Dec 1, 2003.

  1. 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
     
    Elizabeth Harmon, Dec 1, 2003
    #1
    1. Advertising

  2. Elizabeth Harmon

    Alvin Bruney Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
     
    Alvin Bruney, Dec 1, 2003
    #2
    1. Advertising

  3. Thanks for the Advice, i will go with a select statement in the loop so i
    can test for those conditions
    Samantha
    "Alvin Bruney" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
     
    Elizabeth Harmon, Dec 2, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elizabeth Harmon

    Help with Thread.Join() (New Problem)

    Elizabeth Harmon, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    509
    Elizabeth Harmon
    Dec 2, 2003
  2. googlepost
    Replies:
    2
    Views:
    1,624
    Roedy Green
    Jul 22, 2003
  3. lonelyplanet999
    Replies:
    1
    Views:
    523
    John C. Bollinger
    Nov 18, 2003
  4. Alan Silver
    Replies:
    0
    Views:
    914
    Alan Silver
    Jun 5, 2006
  5. googleboy
    Replies:
    1
    Views:
    953
    Benji York
    Oct 1, 2005
Loading...

Share This Page