ASP. NET (a series of programming languages)-multi-thread programming applications under. NET (7)

Source: Internet
Author: User

  Reading directory

 I. Application domain Overview

2. Relationship between application domains and threads

3. The thread spans multiple application domain instances.

Iv. Running Effect

I. Application domain

.In. NET has a feature in. NET has a CLR, there is a CLR between the operating system and our programming environment, there is a kind of isolation between the operating system and CLR, the operating system and the Runtime Library will provide some isolation between applications, to ensure that the code of an application does not affect other unrelated applications, this isolation is required.

.. NET adds an isolation layer called the application domain, which is a logically independent part of the process. That is to say, in order to avoid security issues between two processes, an application error does not affect other applications. For example, if an application error occurs in Windows, a dialog box appears asking if debugging is required, close the application with an error in the taskbar, indicating that the two processes are not associated with each other because there is an application domain between them.

.Common Language Runtime libraries can use application domains to provide isolation between applications

  .The application domain can be understood as my own website. For example, if there is only one road and there is a big truck, private car, or pedestrian in it, the accident of a big truck does not have to affect private cars and pedestrians, in the same way, any of the three will affect the other two because they are in one road. Now we assume there are three lanes, the fast lane is a big truck, and the slow lane is a private small car, if an accident occurs in a big truck when the sidewalk leaves, it will not affect private cars and pedestrians. Then, if a big truck does not want to drive on its own road, it will be okay if it is switched upside down, private cars are similar to pedestrians because they are on three different roads. The three lanes here are equivalent to application domains, while large trucks, private cars, and pedestrians are equivalent to three processes.

.Use application domains
-You can stop an application without stopping the entire process. A process contains many applications. You can stop an application without affecting other applications.
-Code in one application cannot be accessed to directly access code or resources in another application.
-The scope of code behavior is determined by the application in which the code runs.
-Granting code permissions is determined by the application domain in which the code runs.

  

2. Relationship between application domains and threads

  .During runtime, all managed code is loaded into an application domain and run by a specific operating system thread.

  .Application domains and threads do not correspond one by one. That is to say, an application domain can execute multiple threads, and a thread can span multiple application domains, but at a certain point in time, A thread is still in an application domain

  .By calling the Thread. GetDomain method, you can determine the domain where the Thread executes.

3. The thread spans multiple application domain instances.

  Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading;

Namespace AppDomainAndThread
{
Class Program
{
Static void Main (string [] args)
{
// Create an application domain
AppDomain appdomain = AppDomain. CreateDomain ("MyAppDomain ");
Appdomain. SetData ("DomainKey", "DomainValue ");
CommonMethod ();
// CrossAppDomainDelegate it is a delegate, which is called across application domains,
CrossAppDomainDelegate mydelegate = new CrossAppDomainDelegate (CommonMethod );
// The DoCallBack method of AppDomain is used to call applications. Code Execution in another application domain is our output content.
Appdomain. DoCallBack (mydelegate );
Console. ReadKey ();
}

/// <Summary>
/// Method to be called in both application domains
/// </Summary>
Private static void CommonMethod ()
{
// Obtain the current application domain
AppDomain appdomain = AppDomain. CurrentDomain;
Console. writeLine ("the value of the application domain is: '" + appdomain. getData ("DomainKey") + "'in'" + appdomain. friendlyName. the ID of the running thread found in ToString () + "'is'" + AppDomain. getCurrentThreadId (). toString () + "'");
}
}
}

Iv. Running Effect

  We found that the two application domains are respectively appdomainandthread.vshost.exe and MyAppDomain. The threadids of the two are the same, both are 5832, and the same thread is executed in the two application domains.

  

  

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.