diff --git a/src/cascadia/WindowsTerminal/WindowEmperor.cpp b/src/cascadia/WindowsTerminal/WindowEmperor.cpp index 5e5759b117..66d367d976 100644 --- a/src/cascadia/WindowsTerminal/WindowEmperor.cpp +++ b/src/cascadia/WindowsTerminal/WindowEmperor.cpp @@ -646,7 +646,8 @@ void WindowEmperor::_dispatchCommandlineCommon(winrt::array_view mostRecentWeak; + std::shared_ptr mostRecent; + AppHost* window = nullptr; if (winrt::guid currentDesktop; VirtualDesktopUtils::GetCurrentVirtualDesktopId(reinterpret_cast(¤tDesktop))) { @@ -658,19 +659,18 @@ safe_void_coroutine WindowEmperor::_dispatchCommandlineCurrentDesktop(winrt::Ter if (desktopId == currentDesktop && lastActivatedTime > max) { max = lastActivatedTime; - mostRecentWeak = w; + mostRecent = w; } } + + // GetVirtualDesktopId(), as current implemented, should always return on the main thread. + _assertIsMainThread(); + window = mostRecent.get(); } - - // GetVirtualDesktopId(), as current implemented, should always return on the main thread. - _assertIsMainThread(); - - const auto mostRecent = mostRecentWeak.lock(); - auto window = mostRecent.get(); - - if (!window) + else { + // If virtual desktops have never been used, and in turn Explorer never set them up, + // GetCurrentVirtualDesktopId will return false. In this case just use the current (only) desktop. window = _mostRecentWindow(); }