I'm trying to create a dialog from the WindowsAPICodePack in C#, but when it loads, it throws an exception that the calling thread can't access the object.
Exception: System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.Window.get_CriticalHandle()
at System.Windows.Interop.WindowInteropHelper.get_CriticalHandle()
at Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialog.ApplyNativeSettings(IFileDialog dialog)
at Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialog.ShowDialog()
Code that creates exception:
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
CommonFileDialogResult result = dialog.ShowDialog();
I tried using Dispatcher.Invoke, but it still generated the same error. If I use the regular BrowserFolderDialog class it loads, but most likely this one doesn't load because it's from a different .dll file. How can the dialog be created in the same thread?