后台工作人员无法正常工作
我创建了一个后台工作人员来运行一项相当长的任务,其中包括创建更多线程,这些线程将从 url 文件中读取并抓取每个线程。我尝试通过调试跟踪它,发现后台进程没有明显原因提前结束。我的代码逻辑是否有问题导致此问题。我会尝试尽可能多地粘贴以使其有意义。
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
第一段代码是从文件输入的循环,这就是后台工作人员所做的事情。下一段代码是后台工作人员创建线程来处理所有“landingPages”的地方。创建大约 10 个线程后,后台工作程序退出该子进程并跳过文件输入循环并退出程序。
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
我创建后台线程的主程序如下:
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
....
后台工作函数未显示,但基本上在另一个类中调用此函数....
BlogDiscoverObj.start()
现在我尝试等待所有线程的地方在第二个代码块中多于: 暗淡线程 = 新线程(AddressOf processQuery) 计数 = 计数 + 1 thread.Name = "蠕虫" &数数 线程.Start(landingPage) 如果 numThread >= 10 那么 对于 ThreadList 中的每个线程 线程.Join() 下一个 线程数 = 0 继续 别的 线程数 = 线程数 + 1 SyncLock 线程列表 ThreadList.Add(线程) 结束同步锁 结束如果 结束如果 接下来
Thread.Sleep(1000)
For Each Thread In ThreadList
Thread.Join()
Next
希望这更清楚
另外,我的主线程运行一个表单,从该后台调用运行,但主线程应该等待后台进程结束,除非用户从主表单中选择另一个选项。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的主程序是什么样的?您正在启动一些后台线程。只要程序不再存在前台线程,后台线程就会停止。
在退出 Main() 之前,您需要等待启动的线程完成。
How does your main program look like ? You are starting some background threads. A background thread is stopped, whenever no more foreground threads for the program exists.
You need to wait for the threads you started to finish before exiting your Main().