vb.net - axacropdflib - 从工作线程设置源

标签 vb.net multithreading pdf worker-thread

我有一个 vb.net 项目,左侧​​有 pdf TreeView ,右侧有 acrobat AxAcroPDF 查看器控件。单击 TreeView 中的一个项目,我会获取 fileinfo.fullname 值并将其传递给 AxAcroPDF src 属性。

在测试时,我注意到 pdf 的加载速度很慢,并且会阻塞我的 ui 线程,因此我决定工作线程将是在后台延迟加载这些 pdf 的好 helper 。

当我使用工作线程的 DoWork 方法运行代码并尝试更新我的 pdfviewer 对象时,我收到无效的强制转换异常。

System.InvalidCastException was caught HResult=-2147467262
Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'AcroPDFLib.IAcroAXDocShim'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3B813CE7-7C10-4F84-AD06-9DF76D97A9AA}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). Source=mscorlib StackTrace: at 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_treeview_with_filesfolders_docked_andthreads.LoadPDFInBackground(String selectedfile) in C:\Users\me\Desktop.....\fill_treeview_with_filesfolders_docked_andthreads.vb:line 84 InnerException:

我在网上找不到任何其他带有此异常详细信息的线程,所以我不确定这里的问题是什么。我认为我的问题与跨线程访问冲突有关,但即使我将 Control.Checkforillegalcrossthreadcalls 设置为 false,我也会得到相同的异常。无论如何,我从 DoWork 例程中检查 invokerequired 对我来说是没有意义的,因为我的工作线程的目的是为我处理负载,而不是将其推回到 UI 线程中。

任何人都可以推荐一个解决方法,我可以尝试实现我在这里追求的目标吗?

我的代码:

选择后的 TreeView 连接到显示文件

 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

后台 worker 的东西:

#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

最佳答案

只是一个想法,但尝试更改此行:

pdfviewer.src = selectedfile

以下内容:

If pdfviewer.InvokeRequired Then
    pdfviewer.Invoke(Sub() pdfviewer.src = selectedfile)

它可能会解决该错误。看看是否确实如此很有趣。

关于vb.net - axacropdflib - 从工作线程设置源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19820979/

相关文章:

vb.net datagridview不显示所选行

java - Clip.isRunning() 不起作用

java - Java 线程示例中的同步困惑

java - 开源 OCR

jquery - 如何使用 jQuery $.ajax 打开没有回发的 pdf?

.net - 如何防止 Visual Studio Windows 窗体设计器删除控件?

asp.net - 使用相同变量 with/if 语句绑定(bind)不同的 LINQ 数据源

javascript - selectedIndexChanged 之后的 control.focus()

python - 在 Python 中退出线程

javascript - 如何调用memorystream到浏览器下载?