我有一个 vb.net 项目,左边有一个 pdf 的树形视图,右边有一个 acrobat AxAcroPDF 查看器控件。单击树视图中的一个项目,我得到 fileinfo.fullname 值并将其传递给 AxAcroPDF src 属性。
在测试时,我注意到 pdf 的加载速度很慢,并且会阻塞我的 ui 线程,所以我认为工作线程将是在后台延迟加载这些 pdf 的好帮手。
当我使用工作线程的 DoWork 方法运行我的代码并尝试更新我的 pdfviewer 对象时,我得到一个无效的强制转换异常。
System.InvalidCastException 被捕获 HResult=-2147467262
消息=无法将“System.__ComObject”类型的 COM 对象转换为接口类型“AcroPDFLib.IAcroAXDocShim”。此操作失败,因为 IID 为“{3B813CE7-7C10-4F84-AD06-9DF76D97A9AA}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(HRESULT 异常:0x80004002 (E_NOINTERFACE)) . Source=mscorlib StackTrace: 在 System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease) at AcroPDFLib.IAcroAXDocShim.set_src(String pVal) at AxAcroPDFLib.AxAcroPDF.set_src(String value) at myapp.fill_treesview_with_file .LoadPDFInBackground(String selectedfile) 在 C:\Users\me\Desktop .....
我无法在网上找到具有此异常详细信息的任何其他线程,因此我不确定这里的问题是什么。我认为我的问题与跨线程访问冲突有关,但即使我将 Control.Checkforillegalcrossthreadcalls 设置为 false,我也会遇到相同的异常。无论如何,我会从 DoWork 例程中检查 invokerequired 对我来说没有任何意义,因为我的工作线程的重点是为我处理负载,而不是将其推回 UI 线程。
谁能推荐一个解决方法,我可以尝试在这里实现我的目标?
我的代码:
选择后的树视图连接到显示文件
AddHandler TreeView.AfterSelect, AddressOf displayfile
Private Sub displayfile(sender As Object, e As TreeViewEventArgs)
Try
Dim selectedfile As FileInfo = New FileInfo(e.Node.Tag) 'tag has our full path embedded.
'todo: Future - consider type of the file and load a pre-made panel with appropriate host object
If selectedfile.Extension.ToLower.Equals(".pdf") Then
'show "loading...."
LoadingPanel.BringToFront()
backgroundworker.RunWorkerAsync(selectedfile.FullName)
End If
Catch ex As Exception
End Try
End Sub
后台工作人员的东西:
#Region "Background Worker Events"
' This event handler is where the time-consuming work is done.
Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles backgroundworker.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
e.Result = LoadPDFInBackground(e.Argument)
End Sub
' This event handler updates the progress.
Private Sub backgroundWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) Handles backgroundworker.ProgressChanged
ProgressBar.Value = e.ProgressPercentage
End Sub
' This event handler deals with the results of the background operation.
Private Sub backgroundWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) Handles backgroundworker.RunWorkerCompleted
If e.Result Then
'hide loading panel and show pdf panel
pdfviewer.BringToFront()
Else
'what to do if failed to load???
End If
End Sub
#End Region
Private Function LoadPDFInBackground(ByVal selectedfile As String) As Boolean
Try
pdfviewer.src = selectedfile
Return True
Catch ex As Exception
Return False
End Try
End Function