Skip to content

Commit

Permalink
Merge pull request #2835 from freakboy3742/async-startup
Browse files Browse the repository at this point in the history
Modify startup on Windows and macOS so event loop is running when startup is called
  • Loading branch information
freakboy3742 authored Sep 13, 2024
2 parents b0ab9be + e6e7f06 commit fed0c7c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions changes/2834.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions cocoa/src/toga_cocoa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/handlers/handlers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions winforms/src/toga_winforms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fed0c7c

Please sign in to comment.