vb.net线程无法启动

时间:2017-01-11 14:50:57

标签: vb.net multithreading

我写了一个小网络蜘蛛来检查内部网站是否正确地返回搜索结果(大约每天一次它不是出于各种原因)。它运行查找串行,但需要很长时间,所以我希望将它的一些部分拆分成独立的线程。

下面的代码似乎永远不会启动“ripitems”功能,这会留下一个文件让我以后读入excel。

Sub doSearch()
   Dim myVar As System.Collections.ObjectModel.ReadOnlyCollection(Of IWebElement)

   Dim csvFile As String, myContinue As Boolean
   Dim objWriter As TextWriter
   Dim myDriver As New Chrome.ChromeDriver

   csvFile = My.Computer.FileSystem.SpecialDirectories.MyDocuments _
            & "\Catalogue Download - " & Format(Now(), "yyyymmdd") & ".csv"
   objWriter = TextWriter.Synchronized(File.AppendText(csvFile))

   For i = 2 To 10
        myDriver.Navigate.GoToUrl("mysite/search?QueryExpr=" & searchFor & "&pid=" & i)
        myVar = myDriver.FindElementsByClassName("upperContainer")
        Dim myThread As New Thread(Sub() ripitems(myVar, objWriter))
        myThread.Start()
   Next
End Sub


Function ripitems(ByVal elementCollection As System.Collections.ObjectModel.ReadOnlyCollection(Of IWebElement), ByVal fHandle As TextWriter)
    [... irrelevant code to find items and prices ...]
    For i = 0 To elementCollection.Count - 1
        fHandle.Write(thisPN(i) & "," & thisPrice(i))
    Next
End Function

我从this SE answer中删除了大部分线程代码, 我做错了什么?

由于

1 个答案:

答案 0 :(得分:1)

此方法不应为bobTimes

Function

而是将Function ripitems(ByVal elementCollection As System.Collections.ObjectModel.ReadOnlyCollection(Of IWebElement), ByVal fHandle As TextWriter) '[... irrelevant code to find items and prices ...] For i = 0 To elementCollection.Count - 1 fHandle.Write(thisPN(i) & "," & thisPrice(i)) Next End Function 替换为Function

Sub