From 93ec7701b9c522ee60597d85794394fc568a75de Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 13 Sep 2024 07:58:29 +0800 Subject: [PATCH 1/3] Add changenote. --- changes/2834.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/2834.bugfix.rst diff --git a/changes/2834.bugfix.rst b/changes/2834.bugfix.rst new file mode 100644 index 0000000000..d205b2b00d --- /dev/null +++ b/changes/2834.bugfix.rst @@ -0,0 +1 @@ +The event loop is now guaranteed to be running when your app's `startup()` method is invoked. This wasn't previously the case on macOS and Windows. From de7bf241f413cd82dcee4146b37a0da1f7ca59de Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 13 Sep 2024 08:01:50 +0800 Subject: [PATCH 2/3] Defer calling startup until the event loop is running on macOS and Windows. --- cocoa/src/toga_cocoa/app.py | 4 ++-- examples/handlers/handlers/app.py | 4 ++-- winforms/src/toga_winforms/app.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cocoa/src/toga_cocoa/app.py b/cocoa/src/toga_cocoa/app.py index 663bd47088..e2443796a6 100644 --- a/cocoa/src/toga_cocoa/app.py +++ b/cocoa/src/toga_cocoa/app.py @@ -107,8 +107,8 @@ def __init__(self, interface): # Create the lookup table for commands and menu items self._menu_items = {} - # Call user code to populate the main window - self.interface._startup() + # Populate the main window as soon as the event loop is running. + self.loop.call_soon_threadsafe(self.interface._startup) ###################################################################### # Commands and menus diff --git a/examples/handlers/handlers/app.py b/examples/handlers/handlers/app.py index 617eb82b9f..cc7887a1e3 100644 --- a/examples/handlers/handlers/app.py +++ b/examples/handlers/handlers/app.py @@ -44,7 +44,7 @@ async def do_async(self, widget, **kwargs): self.async_label.text = "Ready." widget.enabled = True - async def do_background_task(self, widget, **kwargs): + async def do_background_task(self): """A background task.""" # This task runs in the background, without blocking the main event loop while True: @@ -64,7 +64,7 @@ def startup(self): # Add a background task. self.counter = 0 - self.add_background_task(self.do_background_task) + asyncio.create_task(self.do_background_task()) # Buttons btn_style = Pack(flex=1) diff --git a/winforms/src/toga_winforms/app.py b/winforms/src/toga_winforms/app.py index de1fd96793..54194a20a5 100644 --- a/winforms/src/toga_winforms/app.py +++ b/winforms/src/toga_winforms/app.py @@ -123,8 +123,8 @@ def create(self): "You may experience difficulties accessing some web server content." ) - # Call user code to populate the main window - self.interface._startup() + # Populate the main window as soon as the event loop is running. + self.loop.call_soon_threadsafe(self.interface._startup) ###################################################################### # Commands and menus From e6e7f06f6096554dda078f8ce243adeb6de45ce8 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 13 Sep 2024 09:34:29 +0800 Subject: [PATCH 3/3] Correct markup in change note. Co-authored-by: Russell Martin --- changes/2834.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changes/2834.bugfix.rst b/changes/2834.bugfix.rst index d205b2b00d..8417b0abf6 100644 --- a/changes/2834.bugfix.rst +++ b/changes/2834.bugfix.rst @@ -1 +1 @@ -The event loop is now guaranteed to be running when your app's `startup()` method is invoked. This wasn't previously the case on macOS and Windows. +The event loop is now guaranteed to be running when your app's ``startup()`` method is invoked. This wasn't previously the case on macOS and Windows.