|
-
Apr 30th, 2007, 09:31 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Apr 30th, 2007, 09:37 PM
#2
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.
-
Apr 30th, 2007, 11:14 PM
#3
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|