Sorry for this AI summary but I am a real person.
I am building a Swift 6 dynamic library (DLL) for Windows that is consumed by a .NET 8 application through a C ABI/PInvoke interface.
The goal is to share a Swift business logic layer between macOS and Windows.
The basic architecture is:
.NET 8 application (C#)
|
| P/Invoke
v
Swift dynamic library (DLL)
The DLL loads successfully and synchronous exported C functions execute correctly.
The issue appears when using Swift concurrency.
A function called from the C# application receives data from a Windows callback and needs to update Swift state that is isolated to the MainActor.
A simplified example:
private func runOnMainActor(_ operation: @escaping @MainActor () -> Void)
{
Task { @MainActor in
operation()
}
}
The Task is created, but the MainActor closure does not execute.
I also tested:
Task.detached
{
await MainActor.run
{
operation()
}
}
The detached task starts, but the MainActor.run closure does not execute.
A normal Swift Task executes correctly. The issue appears specific to getting work scheduled onto the MainActor.
An additional observation:
During initial DLL entry from the calling thread, MainActor.assumeIsolated works. However, later calls originating from other .NET callback threads cannot successfully execute MainActor work.
Environment:
- Swift 6.3.2
- Windows ARM64
- .NET 8 application host
- Visual Studio 2022
- Swift dynamic library built with Swift Package Manager
Question:
When Swift concurrency is used inside a Windows DLL hosted by another runtime (.NET in this case), is the MainActor expected to be available automatically?
Does MainActor require the host application to provide a main thread, event loop, or other runtime integration?
What is the recommended pattern for a Swift library that needs actor isolation when called from a foreign runtime such as .NET?
Should a Swift DLL avoid MainActor and provide its own synchronization/executor, or is there a supported way to initialize and use Swift concurrency in this scenario?
Adding, I have a SwiftUI app on macOS that successfully runs all of the core functionality.