Question

I have a vb.net project which has a treeview of pdf's on the left and the acrobat AxAcroPDF viewer control on the right. Click a item in the treeview, I get the fileinfo.fullname value and pass that over to the AxAcroPDF src property.

While testing I noticed that pdf's were slow to load and would block my ui thread so I decided that a workerthread would be a good helper to lazy load these pdf's in the background.

When I run my code with the worker thread's DoWork method and it tries to update my pdfviewer object I get an invalid cast exception.

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:

I can't find any other threads online with this exception detail so I am not sure what the issue is here. I thought my problem had to do with a cross thread access violation but even if I set Control.Checkforillegalcrossthreadcalls to false I get the same exception. It didn't make sense to me that I would check for invokerequired from the DoWork routine anyways because the point of my worker thread is to handle the load for me, not shove it back into the UI thread.

Can anyone recommend a workaround that I can try to achieve what I am after here?

my Code:

The treeview afterselect is wired to displayfile

 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

Background Worker Stuff:

#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
Was it helpful?

Solution

Just a thought, but try changing this line:

pdfviewer.src = selectedfile

to the following:

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

It might work around the error. Interesting to see if it does.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top