From 19cf7483fe448af448932f1c5aa1213a1d7b9db4 Mon Sep 17 00:00:00 2001 From: Antonio QUARTA Date: Fri, 23 May 2025 13:04:06 +0200 Subject: [PATCH] fix The calling thread must be STA exception Inner exception: The calling thread must be STA, because many UI components require this. --- pyproject.toml | 1 + src/pyrenode3/inits.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ea63994..39a6931 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ dynamic = ["version"] dependencies = [ "pythonnet>=3.0.1", + "pythoncom>=308; os_name == 'nt'", ] [project.optional-dependencies] diff --git a/src/pyrenode3/inits.py b/src/pyrenode3/inits.py index 3e93ad6..0ab8115 100644 --- a/src/pyrenode3/inits.py +++ b/src/pyrenode3/inits.py @@ -1,6 +1,9 @@ import atexit import time from threading import Thread +import os +if os.name == "nt" + import pythoncom from Antmicro.Renode import Emulator from Antmicro.Renode.Analyzers import LoggingUartAnalyzer @@ -46,6 +49,16 @@ def __clean(self): c() +def start_main_thread(): + """ start ExecuteAsMainThread """ + if os.name == "nt": + pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED) + Emulator.ExecuteAsMainThread() + pythoncom.CoUninitialize() + else: + Emulator.ExecuteAsMainThread() + + class EmulatorInit(metaclass=MetaSingleton): """A class used for initializing the emulator.""" @@ -54,7 +67,7 @@ def __init__(self): Emulator.ShowAnalyzers = True - self.__thread = Thread(target=Emulator.ExecuteAsMainThread, daemon=True) + self.__thread = Thread(target=start_main_thread, daemon=True) self.__thread.start() Cleaner().add_multiple(