Results 1 to 3 of 3

Thread: [RESOLVED] [2005] Waiting for a thread to finish.

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [RESOLVED] [2005] Waiting for a thread to finish.

    Code:
        Public Delegate Sub SetText(ByVal sText As String)
        Public Class Recurse
            Public sDirectory As String
            Public TextDelegate As SetText
    
            Public Sub Recurse()
                TextDelegate(sDirectory)
            End Sub
        End Class
    
    
                    Dim r As New Recurse
                    r.sDirectory = tbSearchFolder.Text
    
                    If cbSearchSubDirectories.Checked Then
                        r.TextDelegate = AddressOf RecurseDirectories
                        Dim RecurseThread As New Threading.Thread(AddressOf r.Recurse)
                        RecurseThread.Name = "RecurseThread"
                        RecurseThread.IsBackground = True
                        RecurseThread.Start()
                    End If
    I do not want to run anymore code until the thread finishes. I still want the app to be active, but I want to wait until the thread finishes it's process before continuing. How would I do this?

    By the way, I am making a File Searching program.
    Prefix has no suffix, but suffix has a prefix.

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

    Re: [2005] Waiting for a thread to finish.

    What you're asking for is not possible directly, but it is indirectly. When you call Start on the Thread it will start the thread and continue. You can call Thread.Join to wait at that point for the thread to finish but your main thread will freeze until that happens.

    Basically what you need to do is have the call to Thread.Start as the last line in the current method. When the worker thread completes it can invoke a method and you can continue working.

    I suggest that you use a BackgroundWorker because it is specifically designed to make this sort of thing easier. You call RunWorkerAsync, which rasies the DoWork event in a worker thread. You do your work in DoWork event handler. When the DoWork event handler completes the worker thread terminates. At that point the RunWorkerCompleted event is raised in the UI thread and you can continue your main processing from the RunWorkerCompleted event handler.
    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 Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Waiting for a thread to finish.

    Thank you for your reply. I figured out it had to be the last line of the method right before I checked back here.
    Prefix has no suffix, but suffix has a prefix.

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