Results 1 to 6 of 6

Thread: Thread problem!!Help me

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    3

    Unhappy Thread problem!!Help me

    I have code like below:

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    Try
    DO something

    Dim th1 As New Thread(AddressOf ThreadStart)
    th1.IsBackground = True
    'End If
    th1.Start()


    Catch ex As Exception
    DO something
    End Try

    End Sub
    When I click Button, as thread starts by th1.start, it starts thread without ending btnConnect_Click.
    It starts thread, executes few statements of "ThreadStart" and then come back to btnConnect_Click and ends it then again move to thread "ThreadStart" , execute few statement and terminate without any prompt/error.

    Can anyone help me.

    Thanks.
    Last edited by memo421; Jan 3rd, 2009 at 12:09 PM.

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Thread problem!!Help me

    What are you doing in ThreadStart?

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    3

    Re: Thread problem!!Help me

    I am willing code execution in this way:

    1. Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    2.


    3.

    Try

    4.

    Do all the button click function here and after that call the thread

    5.


    Dim th1 As New Thread(AddressOf ThreadStart)

    6.


    th1.IsBackground = True

    7.

    th1.Start()

    8. th1.Join()

    Catch ex As Exception
    DO something
    9. End Try
    10. End Sub

    11. Private Sub Thread()

    12. Do Something

    13. end sub

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Thread problem!!Help me

    Why do you not show us your real code?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Thread problem!!Help me

    I see that Join you have in there, but it wasn't in the first snippet. Was the first snippet real code, or was that left out?
    My usual boring signature: Nothing

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Thread problem!!Help me

    Based on his description, i'll bet that th1.Join() is in his code.

    memo, th1.Join() causes your current thread to spinwait until the other thread has finished its work. It then continues from that line. That's why your function is not exiting. Why are you using Join()?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width