I have created a background worker to go and run a pretty long task that includes creating more threads which will read from a file of urls and crawl each. I tried following it through debugging and found that the background process ends prematurely for no apparent reason. Is there something wrong in the logic of my code that is causing this. I will try and paste as much as possible to make sense.
While Not myreader.EndOfData
Try
currentRow = myreader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
itemCount = itemCount + 1
searchItem = currentField
generateSearchFromFile(currentField)
processQuerySearch()
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
Console.WriteLine(ex.Message.ToString)
End Try
End While
This first bit of code is the loop to input from file and this is what the background worker does. The next bit of code is where the background worker creates threads to work all the 'landingPages'. After about 10 threads are created the background worker exits this sub and skips the file input loop and exits the program.
Try
For Each landingPage As String In landingPages
pgbar.Timer1.Stop()
If VisitedPages.Contains(landingPage) Then
Continue For
Else
Dim thread = New Thread(AddressOf processQuery)
count = count + 1
thread.Name = "Worm" & count
thread.Start(landingPage)
If numThread >= 10 Then
For Each thread In ThreadList
thread.Join()
Next
numThread = 0
Continue For
Else
numThread = numThread + 1
SyncLock ThreadList
ThreadList.Add(thread)
End SyncLock
End If
End If
Next
My main program where I create the background thread is as follows:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
isClicked = True
ProgressBar1.Value = 10
Me.BackgroundWorker1.RunWorkerAsync()
Timer1.Interval = 10000
....
background do work function is not shown but basically calls this function in another class....
BlogDiscoverObj.start()
now where I tried to wait for all the threads was here in the second block of code above: Dim thread = New Thread(AddressOf processQuery) count = count + 1 thread.Name = "Worm" & count thread.Start(landingPage) If numThread >= 10 Then For Each thread In ThreadList thread.Join() Next numThread = 0 Continue For Else numThread = numThread + 1 SyncLock ThreadList ThreadList.Add(thread) End SyncLock End If End If Next
Thread.Sleep(1000)
For Each Thread In ThreadList
Thread.Join()
Next
hope this is clearer
Also my main thread runs a forms from where this background is called to run but the main thread is supposed to wait for the background process to end unless the user selects another option from the main form.