Cyro
Cyro
CC#
Created by Cyro on 8/28/2024 in #help
How does one typically handle complex program flow?
No description
45 replies
CC#
Created by Cyro on 8/8/2024 in #help
Searching for the best way to dynamically load and unload assemblies in .NET Framework
So I'm working in a codebase that is somewhat mixed right now between .NET 8 (or higher) and .NET Framework 4.7.2. I'm interested in loading a DLL, executing code from that DLL, and then unloading it. In newer .NETs, assembly load context works remarkably. I can load an assembly, invoke a function from it, and then immediately unload the context which will wait for everything inside of it to eventually stop running (spawned threads, longer-lived objects, etc.) and get GC'd after some time when all of the references die. However, it appears the way to do this is through AppDomains in .NET Framework. This involves creating a remote object in the main domain, invoking whatever I want to run inside of the new domain from that object, and then unloading the domain. The problem is that unloading the new domain will force all existing threads running in that domain to abort, which is not all that handy if I want to dynamically load code that could potentially spawn threads that run for some time. Is there perhaps some way to track what's all running in an AppDomain and only unload it once everything is said and done?
27 replies