背景工作者不正常工作

时间:2010-04-27 16:52:22

标签: vb.net visual-studio backgroundworker

我已经创建了一个后台工作程序来运行一个非常长的任务,其中包括创建更多线程,这些线程将从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()

现在我试图等待所有线程在上面的第二个代码块中:                Dim thread = New Thread(AddressOf processQuery)                             count = count + 1                             thread.Name =“蠕虫”&计数                             thread.Start(的LandingPage)                             如果numThread> = 10那么                                 对于ThreadList中的每个线程                                     的Thread.join()                                 下一个                                  numThread = 0                                 继续                             其他                                 numThread = numThread + 1                                 SyncLock ThreadList                                     ThreadList.Add(线程)                                 结束SyncLock                             万一                         万一                        下一步

           Thread.Sleep(1000)
           For Each Thread In ThreadList
                Thread.Join()
            Next

希望这更清楚

此外,我的主线程运行一个表单,从该表单调用此后台运行,但主线程应该等待后台进程结束,除非用户从主表单中选择另一个选项。

1 个答案:

答案 0 :(得分:2)

您的主程序如何?你正在开始一些背景线程。只要不存在程序的前台线程,就会停止后台线程。

在退出Main()之前,您需要等待开始完成的主题。