Skip to content

Commit

Permalink
Merge pull request #1522 from freakboy3742/win-error-handling
Browse files Browse the repository at this point in the history
Improve top-level exception handling in Windows apps.
  • Loading branch information
freakboy3742 committed Jul 13, 2022
2 parents 31d16bb + 1da3a81 commit a0788a9
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/winforms/toga_winforms/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import re
import sys
import traceback

import toga
from toga import Key
Expand Down Expand Up @@ -215,18 +214,31 @@ def run_app(self):
try:
self.create()

# This catches errors in handlers, and prints them
# in a usable form.
self.native.ThreadException += self.winforms_thread_exception

self.loop.run_forever(self.app_context)
except: # NOQA
traceback.print_exc()
except Exception as e:
# In case of an unhandled error at the level of the app,
# preserve the Python stacktrace
self._exception = e
else:
self._exception = None

def main_loop(self):
thread = Threading.Thread(Threading.ThreadStart(self.run_app))
thread.SetApartmentState(Threading.ApartmentState.STA)
thread.Start()
thread.Join()

# If the thread has exited, the _exception attribute will exist.
# If it's non-None, raise it, as it indicates the underlying
# app thread had a problem; this is effectibely a re-raise over
# a thread boundary.
if self._exception:
raise self._exception

def show_about_dialog(self):
message_parts = []
if self.interface.name is not None:
Expand Down

0 comments on commit a0788a9

Please sign in to comment.