Trying to create a WPF caliburn micro application that creates a separate desktop and shows its main window there.
Problem: after the desktop is created and i switch there, no window is shown.
namespace WpfThreads
{
class AppBootstrapper : Bootstrapper<WpfThreads.ViewModels.WpfThreadsViewModel>
{
protected override void OnStartup(object sender, StartupEventArgs e)
{
var desktop = Native.CreateDesktop("NewDesktop", 0, 0, 0, DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU | DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK | DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP, 0);
Native.SetThreadDesktop(desktop);
Native.SwitchDesktop(desktop);
base.OnStartup(sender, e);
}
}
}
SetThreadDesktop() fails, other calls are successful. The OnStartup() method does run on main thread (which is also the UI thread).
SetThreadDesktop
succeed? Things as seemingly unrelated asSTAThreadAttribute
can create windows, and "The SetThreadDesktop function will fail if the calling thread has any windows or hooks on its current desktop". – ExitCurrentThread.ApartmentState
, after changing desktop, but before creating any windows. See https://mcmap.net/q/466117/-c-winforms-how-to-set-main-function-stathreadattribute/103167 – ExitSetThreadDesktop
won't use COM, it callsCoInitializeEx
to set the apartment model... thus creating a window at just the wrong time and ensuring thatSetThreadDesktop
fails. – Exit