Results 1 to 4 of 4

Thread: Threading help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    18

    Threading help

    Hi i got a thread that goes like that.

    VB Code:
    1. dim thrd as System.Threading.Thread
    2. thrd = New Threading.Thread(AddressOf ReceiveDataLoop)
    3.         running = True
    4.         thrd.Start()
    5.     End Sub
    6.     Private Sub ReceiveDataLoop()
    7.         While (running)
    8.             receiveData()
    9.             Thread.Sleep(500)
    10.         End While
    11.     End Sub

    when i want to abort the thread it will hang at the .abort method why is it so? how do i solve it?

    VB Code:
    1. ' the thread called method
    2. Public Sub receiveData()
    3.         Dim statusck As Integer
    4.         networkStream = tcpClient.GetStream()
    5.         Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    6.  
    7.         If networkStream.CanRead = True Then
    8.             networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    9.             Dim returndata As String = Encoding.ASCII.GetString(bytes, 0, bytes.Length)
    10.             If (returndata.ToString <> "") Then
    11.                 If (returndata.ToString = 9) Then
    12.                     Me.WindowState = FormWindowState.Minimized
    13.                     Me.NotifyIcon1.Visible = True
    14.                     EnableLowLevelKeys(True)
    15.                 ElseIf (returndata.ToString = 1) Then
    16.                     Me.WindowState = FormWindowState.Maximized
    17.                     Me.NotifyIcon1.Visible = False
    18.                     Me.Visible = True
    19.                     Me.Show()
    20.                     EnableLowLevelKeys(False)
    21.                 ElseIf (returndata.ToString = 0) Then
    22.                     stopWhile()
    23.                     MsgBox(running)
    24.                     Disconnect()
    25.                     Me.NotifyIcon1.Visible = False
    26.                     MsgBox("I am here 1")
    27.                     thrd.Abort() ' always hang here
    28.                     MsgBox("I am here 2")
    29.                     Application.Exit()
    30.                 End If
    31.             End If
    32.         End If
    33.         networkStream.Flush()
    34.     End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading help

    Have you read about the Thread.Abort method? It throws an exception inside your thread that you are supposed to handle and perfrom any required cleanup operations before allowing the thread to terminate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    18

    Re: Threading help

    so when the info say throws an exception, we can put the things to handle in the catch. thanks for the help

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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