If I run this code, will each AppDomain
execute in a different thread?
ThreadPool.QueueUserWorkItem(delegate
{
/// Create AppDomain and run code
});
AppDomains do not get their own thread per default. You may execute code in another AppDomain using existing threads or call a method in the AppDomain, that creates new thread(s). In fact, unless you specifically creates additional threads calling code in another domain will execute on the process' main thread.
From the AppDomain documentation
Multiple application domains can run in a single process; however, there is not a one-to-one correlation between application domains and threads. Several threads can belong to a single application domain, and while a given thread is not confined to a single application domain, at any given time, a thread executes in a single application domain.
In your example, you create threads (or more specifically the thread pool does so) and thus the code will run on these threads. However, I am not sure I would recommend creating AppDomains on thread pool threads like that.
Unloading an AppDomain will abort any threads in the AppDomain. I honestly don't know how the thread pool will react to this. More info about unloading here.
An App Domain is something larger than a thread, but smaller than a process. You could think of them as potentially collections of several threads. If an App Domain creates another, new App Domain, the new App Domain will have it's own thread. A thread in one App Domain will never also be part of another App domain, nor will it be allowed to talk directly to threads from other App Domains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With