gpt4 book ai didi

vb.net - System.UnauthorizedAccessException 仅使用多线程

转载 作者:行者123 更新时间:2023-12-01 15:35:13 26 4
gpt4 key购买 nike

我写了一段代码来解析一些 Web 表。

我通过以下代码使用 Internet Explorer 将一些 Web 表放入 IHTMLElementCollection:

TabWeb = IE.document.getelementsbytagname("table")

然后我使用一个 sub 获取包含 IHTMLElementCollection 和一些其他数据的对象:

Private Sub TblParsing(ByVal ArrVal() As Object)
Dim WTab As mshtml.IHTMLElementCollection = ArrVal(0)
'some code
End sub

我的问题是:如果我简单地“调用”这段代码,它就能正常工作:

Call TblParsing({WTab, LiRow})

但是,如果我尝试将它运行到线程池中:

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TblParsing), {WTab, LiRow})

代码失败给我多个

System.UnauthorizedAccessException

这发生在(每个)这些代码行上:

Rws = WTab(RifWT("Disc")).Rows.Length 

If Not IsError(WTab(6).Cells(1).innertext) Then
Ogg_W = WTab(6).Cells(1).innertext

我的目标是在我的子执行解析时导航到另一个网页。

我想澄清一下:

1) 我试图将整个 HTML 发送到 sub 并将其放入 webbrowser 但它没有用,因为它不可能从 System. Windows.Forms.HtmlElementCollectionmshtml.IHTMLElementCollection(或者我做不到);

2) 我不能使用 WebRequest 和类似的:我被迫使用 InternetExplorer;

3) 我无法使用 System.Windows.Forms.HtmlElementCollection,因为我的解析代码使用了 CellsRows 等等不可用(我不想重写我所有的解析代码)

编辑:

好的,我使用如下答案提示修改了我的代码:

'This in the caller sub
Dim IE As Object = CreateObject("internetexplorer.application")
'...some code
Dim IE_Body As String = IE.document.body.innerhtml
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TblParsing_2), {IE_Body, LiRow})
'...some code

'This is the called sub
Private Sub TblParsing_2(ByVal ArrVal() As Object)
Dim domDoc As New mshtml.HTMLDocument
Dim domDoc2 As mshtml.IHTMLDocument2 = CType(domDoc, mshtml.IHTMLDocument2)
domDoc2.write(ArrVal(0))

Dim body As mshtml.IHTMLElement2 = CType(domDoc2.body, mshtml.IHTMLElement2)
Dim TabWeb As mshtml.IHTMLElementCollection = body.getElementsByTagName("TABLE")
'...some code

我没有收到任何错误,但我不确定它是否正确,因为我尝试将 IE_Body 字符串用于 webbrowser,它在网页中抛出错误(它显示一个弹出窗口,我可以忽略错误).

我是否使用正确的方法将 Internet Explorer 中的 Html 转换为 string

编辑2:

我将我的代码更改为:

Dim IE As New SHDocVw.InternetExplorer  
'... some code
Dim sourceIDoc3 As mshtml.IHTMLDocument3 = CType(IE.Document, mshtml.IHTMLDocument3)
Dim html As String = sourceIDoc3.documentElement.outerHTML
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TblParsing_2), {html, LiRow})
'... some code

Private Sub TblParsing_2(ByVal ArrVal() As Object)
Dim domDoc As New mshtml.HTMLDocument
Dim domDoc2 As mshtml.IHTMLDocument2 = CType(domDoc, mshtml.IHTMLDocument2)
domDoc2.write(ArrVal(0))
Dim body As mshtml.IHTMLElement2 = CType(domDoc2.body, mshtml.IHTMLElement2)
Dim TabWeb As mshtml.IHTMLElementCollection = body.getElementsByTagName("TABLE")

但我收到一个错误弹出窗口,例如(我尝试翻译它):
标题:

Web page error

正文:

Debug this page?
This page contains errors that might prevent the proper display or function properly.
If you are not testing the web page, click No.

两个复选框

do not show this message again
Use script debugger built-in Internet Explorer

这与我尝试将 Html 文本输入 WebBrowser 时遇到的错误相同。
但是,如果我可以忽略这个错误,我认为代码可以工作!

当 pop 正在显示时我收到错误

Dim domDoc As New mshtml.HTMLDocument

翻译的错误文本是:

Retrieving the COM class factory for component with CLSID {25336920-03F9-11CF-8FD0-00AA00686F13} failed due to the following error: The 8,001,010th message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)).

请注意,我已经设置了 IE.silent = True

最佳答案

编辑:关于 OP 中“Internet Explorer”的含义存在混淆。我最初认为它是指 WinForm Webbrowser 控件;然而,OP 直接创建了 COM 浏览器,而不是使用 .Net 包装器。

要获取浏览器文档的定义 HTML,您可以针对 mshtml.IHTMLDocument3 接口(interface)转换文档以公开 documentElement 属性。

Dim ie As New SHDocVw.InternetExplorer  ' Proj COM Ref: Microsoft Internet Controls
ie.Navigate("some url")
' ... other stuff
Dim sourceIDoc3 As mshtml.IHTMLDocument3 = CType(ie.Document, mshtml.IHTMLDocument3)
Dim html As String = sourceIDoc3.documentElement.outerHTML

结束编辑。


以下是基于我上面的评论。您使用 WebBrowser.DocumentText property创建 mshtml.HTMLDocument

Use this property when you want to manipulate the contents of an HTML page displayed in the WebBrowser control using string processing tools.

一旦将此属性提取为字符串,就不会连接到 WebBrowser 控件,您可以在任何线程中处理数据。

Dim html As String = WebBrowser1.DocumentText

Dim domDoc As New mshtml.HTMLDocument
Dim domDoc2 As mshtml.IHTMLDocument2 = CType(domDoc, mshtml.IHTMLDocument2)
domDoc2.write(html)

Dim body As mshtml.IHTMLElement2 = CType(domDoc2.body, mshtml.IHTMLElement2)
Dim tables As mshtml.IHTMLElementCollection = body.getElementsByTagName("TABLE")

' ... do something

' cleanup COM objects
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(body)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(tables)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(domDoc)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(domDoc2)

关于vb.net - System.UnauthorizedAccessException 仅使用多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34871080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com