.Net - CapturePicker The application called an interface that was marshalled for a different thread
I'm trying to use GraphicsCapturePicker but I'm getting the following error:
The application called an interface that was marshalled for a different thread
Here is the following methods that I'm using to open the picker:
public async void OpenCapturePicker(object sender, RoutedEventArgs e)
{
Console.WriteLine("OpenCapturePicker");
await Dispatcher.InvokeAsync(OpenCapturePicker);
}
private async void OpenCapturePicker()
{
var picker = new GraphicsCapturePicker();
picker.SetWindow(_capturableWindowHandle);
var item = await picker.PickSingleItemAsync();
if (item == null) return;
//rest of code
}
The _capturableWindowHandle is defined in the constructor of the class like:
public SomeClass(){
_capturableWindowHandle = new WindowInteropHelper(this).Handle;
}
Also I've an extension class that allows me to use the SetWindow method
public static class CaptureHelper
{
static readonly Guid GraphicsCaptureItemGuid = new Guid("79C3F95B-31F7-4EC2-A464-632EF5D30760");
public static GraphicsCaptureItem CreateItemForWindow(IntPtr windowInteropHandler)
{
var factory = WindowsRuntimeMarshal.GetActivationFactory(typeof(GraphicsCaptureItem));
var interop = (IGraphicsCaptureItemInterop) factory;
var itemPointer = interop.CreateForWindow(windowInteropHandler, GraphicsCaptureItemGuid);
var item = Marshal.GetObjectForIUnknown(itemPointer) as GraphicsCaptureItem;
Marshal.Release(itemPointer);
return item;
}
public static void SetWindow(this GraphicsCapturePicker picker, IntPtr hwnd)
{
var interop = (IInitializeWithWindow)(object)picker;
interop.Initialize(hwnd);
}
}
Any ideas?
Comments
Post a Comment