Do AppDomains run in their threads?

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

  ThreadPool.QueueUserWorkItem(delegate { /// Create AppDomain and run code }); 
+7
multithreading c # appdomain
source share
2 answers

AppDomains does not get its own default thread. You can execute code in another AppDomain using existing threads, or call a method in AppDomain that creates new threads. In fact, unless you specifically create additional threads that invoke code in another domain, they will be executed in the main thread of the process.

From the AppDomain Documentation

Multiple application domains can run in one process; however, there is no one-to-one correlation between application domains and threads. Several threads can belong to the same application domain, and while this thread is not limited to one application domain at any given time, the thread runs in the same application domain.

In your example, you create threads (or, more specifically, a thread pool), and in this way the code will work in these threads. However, I'm not sure I would recommend creating AppDomains in thread pool threads like this.

Unloading the AppDomain cancels any threads in the AppDomain. I honestly don’t know how this thread will react to this. Read more about unloading here .

+11
source share

An application domain is more than a stream, but less than a process. You can think of them as a potential collection of multiple threads. If the application domain creates another, new application domain, the new application domain will have its own stream. A thread in one application domain will never be part of another application domain, nor will it be allowed to communicate directly with threads from other application domains.

+3
source share

All Articles