diff options
| author | Miguel Costa <miguel.costa@qt.io> | 2025-11-21 22:46:45 +0100 |
|---|---|---|
| committer | Miguel Costa <miguel.costa@qt.io> | 2025-11-26 18:07:40 +0000 |
| commit | c8293934db671a1216b51005ee052516cee2e391 (patch) | |
| tree | 9b3065b54289a2048a9584e088b678e886413815 | |
| parent | 82a145c651be8b8862287a918dab5b6945663074 (diff) | |
Exit Qt app when .NET thread returns early
Fix app frozen after .NET thread exits before `exec()` of the Qt app.
Change-Id: Iaca005ac7919241ed2435b7f12783405197cb8b2
Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io>
| -rw-r--r-- | src/Qt.DotNet.GenerationRules/Qt/DotNet/CodeGeneration/Rules/GenerateMainCpp.cs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/Qt.DotNet.GenerationRules/Qt/DotNet/CodeGeneration/Rules/GenerateMainCpp.cs b/src/Qt.DotNet.GenerationRules/Qt/DotNet/CodeGeneration/Rules/GenerateMainCpp.cs index c5d7d2e..d3219ca 100644 --- a/src/Qt.DotNet.GenerationRules/Qt/DotNet/CodeGeneration/Rules/GenerateMainCpp.cs +++ b/src/Qt.DotNet.GenerationRules/Qt/DotNet/CodeGeneration/Rules/GenerateMainCpp.cs @@ -71,19 +71,24 @@ int main(int argc, char *argv[]) QStringList args; for (int i = 0; i < argc; ++i) args << argv[i]; + int dotNetResult = -1; + bool dotNetExited = false; auto *dotnetThread = QThread::create( - [&args, &app, &dotNetHost, &assemblyPath]() + [&args, &app, &dotNetHost, &assemblyPath, &dotNetResult, &dotNetExited]() {{ dotNetHost.loadApp(assemblyPath, args); - int result = dotNetHost.runApp(); - app.exit(result); + dotNetResult = dotNetHost.runApp(); + dotNetExited = true; + app.exit(dotNetResult); }}); dotnetThread->start(); int tries = 0; constexpr int maxTries = 10000; // ~1 sec total - while (!dotNetHost.isReady() && tries++ < maxTries) + while (!dotNetExited && !dotNetHost.isReady() && tries++ < maxTries) QThread::usleep(100); + if (dotNetExited) + return dotNetResult; if (!dotNetHost.isReady()) {{ qCritical() << "".NET host not ready after timeout.""; return -2; @@ -99,6 +104,9 @@ int main(int argc, char *argv[]) {mainCpp[new(MainBeforeAppExec) { Sorted = false }]} QObject::connect(&app, &QGuiApplication::aboutToQuit, &qmlEngine, &QQmlEngine::quit); + if (dotNetExited) + return dotNetResult; + auto res = app.exec(); dotnetThread->wait(); #ifdef Q_OS_WINDOWS |
