[VB.NET]网络采集工具的页面内容下载在进程中没错,而在线程的就不行了?

前端之家收集整理的这篇文章主要介绍了[VB.NET]网络采集工具的页面内容下载在进程中没错,而在线程的就不行了?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
网络采集工具的页面内容下载在进程中没错,而在线程的就不行了? 这个是取URL源文件的类: Public Class HtmlSource Private WithEvents WebBrowser As WebBrowser Private Document_Type As String = Public Html_Content As String = Public Function WebBrowserUrl(ByVal Url As Uri,ByVal Web_Type As String) As Boolean Try HtmlCode.Substring(Me.HtmlCode.IndexOf( > ),Me.HtmlCode.IndexOf( < ) - Me.HtmlCode.IndexOf( > )) Dim Falg As Boolean = True Document_Type = Web_Type WebBrowser = New WebBrowser WebBrowser.ScriptErroRSSuppressed = True WebBrowser.Navigate(Url) For i As Integer = 0 To 500 If Html_Content <> Then Return True Falg = False Exit For End If System.Threading.Thread.Sleep(1000) System.Threading.Thread.SpinWait(1000) Windows.Forms.Application.DoEvents() Next If Falg = True Then Return False Catch ex As Exception MsgBox(ex.Message) Return False End Try End Function Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object,ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted Try If WebBrowser.IsBusy = False Then Select Case Document_Type Case All Html_Content = WebBrowser.Document.GetElementsByTagName( html ).Item(0).OuterHtml Case Title Html_Content = WebBrowser.Document.Title.ToString Case Body Html_Content = WebBrowser.Document.Body.OuterHtml Case Links If WebBrowser.Document.Links.Count > 0 Then For i As Integer = 0 To WebBrowser.Document.Links.Count - 1 If i = 0 Then Html_Content = WebBrowser.Document.Links.Item(i).OuterHtml.Replace(vbCrLf,) Else Html_Content = Html_Content & vbCrLf & WebBrowser.Document.Links.Item(i).OuterHtml.Replace(vbCrLf,) End If Next Else Html_Content = End If Case Urls If WebBrowser.Document.Links.Count > 0 Then For i As Integer = 0 To WebBrowser.Document.Links.Count - 1 If i = 0 Then Html_Content = WebBrowser.Document.Links.Item(i).GetAttribute( href ).Trim.ToString.Replace(vbCrLf,) Else Html_Content = Html_Content & vbCrLf & WebBrowser.Document.Links.Item(i).GetAttribute( href ).Trim.ToString.Replace(vbCrLf,) End If Next Else Html_Content = End If Case Else Html_Content = WebBrowser.Document.GetElementsByTagName( html ).Item(0).OuterHtml End Select Else Html_Content = End If Catch ex As Exception Html_Content = End Try End Sub End Class 通过线程掉用该类会出现以下错误提示:当前线程不在单线程单元中,因此无法实例化ActiveX控件“8856f961-340a-11d0-a96b-00c04fd705a2” __________________________________________________________________________ up __________________________________________________________________________ 是线程的问题 __________________________________________________________________________ 是什么问题呀!!能告诉我吗!! __________________________________________________________________________ 看代码似乎没啥问题啊 怪 __________________________________________________________________________ ActiveX对象必须在设置为STA模式的单线程中. 如果是你自己写一段多线程序代码 就可以在线程执行之前设置其线程模型为STA 这样就可以执行了 __________________________________________________________________________ 原文链接:https://www.f2er.com/vb/263482.html

猜你在找的VB相关文章