Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do AppDomains execute in their own threads ?

If I run this code, will each AppDomain execute in a different thread?

 ThreadPool.QueueUserWorkItem(delegate
 {
     /// Create AppDomain and run code
 });
like image 293
CodingThunder Avatar asked Mar 05 '10 16:03

CodingThunder


2 Answers

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.

like image 52
Brian Rasmussen Avatar answered Oct 17 '22 16:10

Brian Rasmussen


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.

like image 39
Joel Coehoorn Avatar answered Oct 17 '22 14:10

Joel Coehoorn