IPdfRendererNative.RenderPageToSurface InvalidCastException in background thread RRS feed

  • Question

  • Hi,

    in my project on C# I use PdfApi via ComWrappers to render documents. When method IPdfRendererNative.RenderPageToSurface is called from UI thread, everything works fine. But in background thread it causes an InvalidCastException. To access IPdfRendererNative in C# Com Interop and Marshaling are used.

    It was tested on Win 8.1 and VS2013 pro. Here link on sample Project

    Steps to reproduce:

    1) Open project and launch app

    So, is it possible to use multithreading rendering in C#?

    Wednesday, November 20, 2013 10:24 AM

All replies

  • Not on a background thread.  You will have to use the Dispatcher to run this on the UI thread.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, November 20, 2013 1:55 PM
  • Not on a background thread.  You will have to use the Dispatcher to run this on the UI thread.

    Ok, I'll ask a bit different.

    This sample uses same APIs. But renderer is implemented using Windows Store App DLL on C++. IPdfRendererNative.RenderPageToSurface works fine in background thread.

    But in the sample above we get InvalidCastException, when method IPdfRendererNative.RenderPageToSurface is called in background thread. Here is StackTrace:

       at System.StubHelpers.InterfaceMarshaler.ConvertToNative(Object objSrc, IntPtr itfMT, IntPtr classMT, Int32 flags)
       at DirectX.IPdfRendererNative.RenderPageToSurface(Object pdfPage, IDXGISurface pSurface, POINT offset, PDF_RENDER_PARAMS& pRenderParams)

    UPD. So, we have two equivalent code sources, in C++ one and in C# another. C++ works fine. Why the absolutely equivalent source code in C# does not work?

    Thursday, November 21, 2013 12:28 PM