Results 1 to 3 of 3

Thread: Simple threading problem... [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    294

    Simple threading problem... [Resolved]

    This is the first day I have tried to use threads. The code below doesnt work for me. It is supposed to call TestSub every 500 ms. I created a blank windows application in VB.NET 2005 Express Edition Beta #2 and paste this directly into the code and run -
    VB Code:
    1. Imports System.Threading
    2. Public Class Form1
    3.     Dim THRD As Thread
    4.  
    5.     Private Shared Sub TestSub()
    6.         MsgBox("Bleh", MsgBoxStyle.Information, "")
    7.         Thread.Sleep(500)
    8.     End Sub
    9.  
    10.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    11.         THRD = New Thread(AddressOf TestSub)
    12.         THRD.Start()
    13.         THRD.IsBackground = True
    14.     End Sub
    15.  
    16. End Class
    When I click the form, it is supposed to initialize and start the thread. So I click the form... and get the messagebox, and wait a few seconds, nothing has happened. I pause the app and find out, via the intermediate window, THRD.IsAlive = False

    Can anyone please tell me what is wrong with this code? I'm sure its probably some simple thing I've overlooked.

    Edit: Sorry, didnt mention, I need this thread to stay alive, I'm using a loop to keep it running (with a sleep statement), but what is the proper way?
    Last edited by Cade; Sep 27th, 2005 at 05:37 AM.

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

    Re: Simple threading problem...

    Well you aren't using a loop in the code you've provided. After the call to Sleep the TestSub method completes and the thread exits. Unless you raise multiple MouseDown events you're only going to get one MessageBox. You should put a Do loop inside the TestSub method if you want to continually get MessageBoxes. You should also provide a While or Until condition on that loop so that you have a way to make it exit. Also, why have you made the TestSub method Shared?
    VB Code:
    1. Private exitThread As Boolean
    2.  
    3. Private Sub TestSub
    4.     Do
    5.         MessageBox.Show("Test message")
    6.         Thread.Sleep(500)
    7.     Loop Until Me.exitThread
    8. End Sub
    9.  
    10. Private Sub Form1_MouseDown(...) Handles MyBase.MouseDown
    11.     Dim thrd As New Thread(AddressOf TestSub)
    12.  
    13.     Me.exitThread = False
    14.     thrd.IsBackground = True
    15.     thrd.Start()
    16. End Sub
    You should provide a way to set exitThread to True to terminate the thread.
    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
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    294

    Re: Simple threading problem...

    Right, thanks
    The code I use from the thread is
    VB Code:
    1. Private Sub CallbackRecvCheck()
    2.         Do While sckTick.IsAlive
    3.             Thread.Sleep(16)
    4.  
    5.             If sckObj.Connected <> bLastConnected Then
    6.                 If sckObj.Connected Then
    7.                     RaiseEvent Connected()
    8.                 Else
    9.                     RaiseEvent Disconnected()
    10.                 End If
    11.                 bLastConnected = sckObj.Connected
    12.             End If
    13.  
    14.             If sckObj.Connected Then
    15.                 If sckObj.GetStream.DataAvailable Then
    16.                     RaiseEvent RecievedDataPending()
    17.                 End If
    18.             End If
    19.  
    20.         Loop
    21.     End Sub
    Loop ends automatically when thread.abort is called

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