HWA: instantiating RenderTargetBitmap throws RPC_E_WRONG_THREAD even in UI thread

1

I'm trying to use RenderTargetBitmap to get a screenshot of a Windows Hosted Web App (HWA) window, but I can't seem to instantiate it when handling a request from the Javascript web app running on the HWA app:

public async Task<IBuffer> CaptureScreenshotToFileAsync(uint thumbnailWidth, uint thumbnailHeight, string fileName)
{
    // Throws RPC_E_WRONG_THREAD
    RenderTargetBitmap screenshotBitmap = new RenderTargetBitmap();

    // Assumed that this was a background thread, but...
    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        // Surprisingly same thread as above, naturally also throws RPC_E_WRONG_THREAD
        RenderTargetBitmap screenshotBitmap = new RenderTargetBitmap();            
    });
}

In both cases, this is thrown:

System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E    (RPC_E_WRONG_THREAD))
       at Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap..ctor()

By the way, if I do the following from a background thread in a standard UWP app, it works as expected:

public MainPage()
{
    // UI thread

    Task.Run(() => {
        // Background thread
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            // Back in UI thread.
            RenderTargetBitmap targetBitmap = new RenderTargetBitmap(); // doesn't throw.
        }
    });
}

Can anyone explain:

  • Why the instantiation of RenderTargetBitmap fails in the "UI" thread in a HWA app?
  • Why a web request from Javascript is handled by the "UI" thread of the HWA app (bonus)?
c#
uwp
rendertargetbitmap
asked on Stack Overflow May 14, 2018 by Gehrard Raven

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0