[src] Clean/consolidate runtime initialization code between platforms.#26131
Conversation
* Add Runtime.EnsureUIThread (), and call this method from [NS|UI]Application.EnsureUIThread. Also move supporting code to the Runtime class. * Remove NSApplication.EnsureInitialized - this was necessary to ensure `xamarin_initialize` was called when Xamarin.Mac was hosted inside another application (such as Visual Studio for Mac). .NET for macOS no longer supports being hosted inside another application, so this code can be removed. * Remove Runtime.RegisterAssemblies () - it's no longer needed because .NET for macOS doesn't support being hosted inside another app. * Don't set MONO_CFG_DIR anymore - this is *old* code, and shouldn't be needed now. If anybody does, then they can set this variable themselves. * Remove NSApplication.ResetHandle () - it's no longer needed because .NET for macOS doesn't support being hosted inside another app. * Add NSApplication.Initialize () - this mirrors UIApplication.Initialize () * Remove NSApplication.InitDrawBridge () - this is not applicable for .NET for macOS (not fully removed until XAMCORE_5_0) * Remove NSApplication.Init () - this is not needed anymore in .NET for macOS (not fully removed until XAMCORE_5_0) * Make the various [NS|UI]Application.Main methods return 'int' in XAMCORE_5_0. * Misc other dead code fixes. * Misc xml documentation updates.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The consolidated Runtime.EnsureUIThread returned early when CheckForIllegalCrossThreadCalls was true, which is exactly when the check should run. This meant the UIKit/AppKit cross-thread access exceptions were never thrown, breaking the monotouch-test DispatchTests on iOS/tvOS/Mac Catalyst (they got a NullReferenceException instead of a UIKitThreadAccessException). Invert the condition so the check runs when CheckForIllegalCrossThreadCalls is enabled. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The consolidation moved the AppKitSynchronizationContext setup out of NSApplication.Init and into Runtime.InitializePlatform, which runs during early native startup (xamarin_initialize). Hosts and the test runner (MacRunner) start the main run loop by calling NSApplication.Init followed by NSApplication.SharedApplication.Run, *without* going through NSApplication.Main. On base, NSApplication.Init established the synchronization context on that run-loop thread; after the consolidation it no longer did, so the context was no longer guaranteed to be current on the thread that actually runs the app. This is the same pattern iOS already uses (UIApplication.Main calls UIApplication.Initialize on the run-loop thread), and it fixes the link-all macOS PrintPreview_NSGraphicsContextCurrentContext hang, where the modal print operation could no longer be aborted via a continuation posted to the (missing) AppKit synchronization context. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
This PR consolidates cross-platform runtime/UI-thread initialization by moving UI-thread enforcement logic into ObjCRuntime.Runtime, and by simplifying AppKit/UIKit initialization paths now that .NET for macOS is no longer hostable inside other applications.
Changes:
- Centralized UI-thread enforcement in
ObjCRuntime.Runtime.EnsureUIThread (), and routedUIApplication.EnsureUIThread/NSApplication.EnsureUIThreadthrough it. - Introduced
NSApplication.Initialize ()(to mirrorUIApplication.Initialize ()) and adjusted macOS runtime platform initialization to call it. - Updated
UIApplication.Main/NSApplication.Mainto returnintunderXAMCORE_5_0and performed associated cleanup of older initialization code paths.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/UIKit/UIApplication.cs | Routes EnsureUIThread to Runtime.EnsureUIThread and updates Main overloads to return int in XAMCORE_5_0. |
| src/ObjCRuntime/Runtime.mac.cs | Calls NSApplication.Initialize () during platform initialization. |
| src/ObjCRuntime/Runtime.iOS.cs | Adds Mac Catalyst product/assembly naming constants and keeps UIApplication.Initialize () as the platform initializer. |
| src/ObjCRuntime/Runtime.cs | Stores main thread during runtime init and introduces centralized EnsureUIThread (). |
| src/AppKit/NSApplication.cs | Adds Initialize (), simplifies Init/Main, and routes EnsureUIThread to Runtime.EnsureUIThread. |
…n.Init." This reverts commit ce719bd.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
/azp run xamarin-macios-sim-pr-tests |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
|
/azp run xamarin-macios-sim-pr-tests |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
|
/azp run xamarin-macios-sim-pr-tests |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
🚀 [CI Build #067c0f3] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 203 tests passed 🎉 Tests counts✅ assembly-processing: All 1 tests passed. [attempt 2] Html Report (VSDrops) Download macOS tests✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
This is a significant code unification between macOS and the other platforms. Xamarin.Mac supported being loaded into a separate app, which meant additional supporting code. macOS for .NET does not support being loaded into a separate app (just like the other platforms), which means all the additional supporting code can be deleted, and we can share much more code between platforms.
xamarin_initializewas called when Xamarin.Mac was hosted inside another application (such as Visual Studio for Mac). .NET for macOS no longer supports being hosted inside another application, so this code can be removed.