In the world of managed code, applications are loaded into the application domain (AppDomain) First, then the application domain is loaded into the process, and a process can contain multiple application domains, which means that a process can contain multiple applications, after all, switching between application domains is lighter than switching between processes.
Data between application domains is independent of each other, and you can use crossappdomaindelegate delegates when you need to execute assembly code in the current AppDomain in other AppDomain. After you delegate the Crocessappdomaindelegate method, the DoCallback method of the AppDomain executes the delegate.
classProgram {Static voidMain (string[] args) {Console.WriteLine ("The main program domain starts running"); //to create a new application domain objectAppDomain Newappdomain = Appdomain.createdomain ("Sub-Program fields"); //delegate method for binding CrossAppDomainDelegateCrossAppDomainDelegate crossappdomaindelegate =Newcrossappdomaindelegate (Mycallback); //the child application domain invokes the delegate, which is equivalent to the method in the subroutine domain calling the main program domainNewappdomain.docallback (crossappdomaindelegate); //Binding program Domain Unload event handling methodNewappdomain.domainunload + = (obj, e) = ={Console.WriteLine ("Sub-program domain uninstall!"); }; //To unload a sub-program domainappdomain.unload (Newappdomain); Console.readkey (); } Static Public voidMycallback () {stringName =AppDomain.CurrentDomain.FriendlyName; for(intn =0; N <4; n++) {Console.WriteLine (string. Format ("Current program runs on {0}", name)); } } }
A process can contain multiple threads, threads exist in the process, and it can run in many different AppDomain at different times. It is the basic unit of execution in the process, and the first thread that executes at the process entrance is considered the main path of the process. In. NET applications, the main () method is used as the portal, and when this method is called, the system automatically creates a main thread. Threads are primarily composed of CPU registers, call stacks, and thread local memory (TLS). The CPU registers primarily record the state of the currently executing thread, and the call stack is primarily used to maintain the memory and data that the thread invokes, and TLS is primarily used to hold the thread's state information.
. Net Framework Awareness