Skip to content

Commit

Permalink
Run the save on a background thread so that we dont block the ui.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosefield committed Aug 29, 2021
1 parent 8042cb8 commit 769eb31
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,9 @@ AppHost::AppHost() noexcept :
_window->MakeWindow();

_windowManager.GetWindowLayoutRequested([this](auto&&, const winrt::Microsoft::Terminal::Remoting::GetWindowLayoutArgs& args) {
if (_windowManager.IsMonarch())
{
// If we are the monarch then the peasant thread is on the ui thread
// and we can just ask directly for the layout.
const auto pos = _GetWindowLaunchPosition();
args.WindowLayoutJson(_logic.GetWindowLayoutJson(pos));
}
else
{
// Otherwise they are on a separate thread, and we need to switch
// contexts to be able to get the json.
args.WindowLayoutJsonAsync(_GetWindowLayoutAsync());
}
// The peasants are running on separate threads, so they'll need to
// swap what context they are in to the ui thread to get the actual layout.
args.WindowLayoutJsonAsync(_GetWindowLayoutAsync());
});

_windowManager.BecameMonarch({ this, &AppHost::_BecomeMonarch });
Expand Down Expand Up @@ -710,7 +700,8 @@ void AppHost::_DispatchCommandline(winrt::Windows::Foundation::IInspectable send
// - Asynchronously get the window layout from the current page. This is
// done async because we need to switch between the ui thread and the calling
// thread.
// - NB: This will fail if called on the monarch, it should only be used for a peasant.
// - NB: The peasant calling this must not be running on the UI thread, otherwise
// they will crash since they just call .get on the async operation.
// Arguments:
// - <none>
// Return Value:
Expand Down Expand Up @@ -810,16 +801,15 @@ void AppHost::_BecomeMonarch(const winrt::Windows::Foundation::IInspectable& /*s

winrt::fire_and_forget AppHost::_SaveWindowLayouts()
{
// Use the main thread since we are accessing controls.
co_await winrt::resume_foreground(_logic.GetRoot().Dispatcher());
// Make sure we run on a background thread to not block anything.
co_await winrt::resume_background();

if (_logic.ShouldUsePersistedLayout())
{
auto layoutJsons = _windowManager.GetAllWindowLayouts();
_logic.SaveWindowLayoutJsons(layoutJsons);
}

co_await winrt::resume_background();
if (_getWindowLayoutThrottler.has_value())
{
// don't need to save too frequently
Expand Down

0 comments on commit 769eb31

Please sign in to comment.