Crash using SharpDX and D3DImage after reconnecting to RDP session

476 Views Asked by At

I use the same approach explained here to avoid the issue with D3DImage.SetBackBuffer() method via RDP, but I found out that when I disconnect and reconnect to the same session, ASA I resize my control I get the exception: Back buffer's device is not valid.

Here is the code I use

public void Update(SharpDX.Direct3D11.Device device, SharpDX.Direct3D11.DeviceContext context,
    Texture2D wpfSharedSurface, IntPtr sharedSurfacePtr)
{
    // Make sure that Lock/Unlock/SetBackBuffer...etc. are called on the UI thread
    context.ResolveSubresource(Surface.RenderTexture, 0, wpfSharedSurface, 0,
        Surface.RenderTexture.Description.Format);

    // Emits the proper Query.End to track the end of rendering for this BeginDraw/EndDraw pair.
    device.ImmediateContext.End(Surface.QuerySync);
    device.ImmediateContext.Flush();

    while (!device.ImmediateContext.IsDataAvailable(Surface.QuerySync))
    {

    }

    // http://msdn.microsoft.com/en-us/library/system.windows.interop.d3dimage.aspx

    D3DImageInstance.Lock();

    D3DImageInstance.SetBackBuffer(D3DResourceType.IDirect3DSurface9, sharedSurfacePtr, true // allow RDP to work 
        );
    D3DImageInstance.AddDirtyRect(new Int32Rect(0, 0, Width, Height));
    D3DImageInstance.Unlock();
}

The below image shows the error that occurs enter image description here

I also tried hints found here and here without luck (as expected)

0

There are 0 best solutions below