The thread is indeed a good thing, allowing you to make multiple different processing at the same time.
An isolation layer has been added in .NET, it is called AppDomain, which is a logical independent part of the process. A plurality of application domains can be present in one process. The application domain can save one or more threads, just like the process. The difference is that the application domain can be created inside the process, but do not create a new thread.
One current problem is: threads can be executed across multiple application domains, why? Look back and look back.
Ok, take a look at Appdomain: appdomain.cs
Using system;
Public class myappdomain
{
Public Appdomain Domain;
Public int threadid;
Public void setdomainData (String vName, String Vvalue)
{
Domain.SetData (vName, (Object) vValue;
Threadid = Appdomain.getCurrentThreadId ();
}
Public string getdomainData (String name)
{
Return (String) Domain.getdata (Name);
}
Public static void main ()
{
String DataName = "mydata";
String DataValue = "Some Data to Be Stored";
Console.WriteLine ("Retrieving Current Domain);
MyAppdomain Obj = new myappdomain ();
Obj.domain = appdomain.currentdomain
Console.WriteLine ("Setting Domain Data");
Obj.seetdomainData (Dataname, DataValue);
Console.writeline ("Getting Domain Data");
Console.writeline ("The Data Found for Key ' Dataname "' is '" Obj.getDomainData (Dataname) ' Running On ThreadId:" Obj.threadID);
}
}
This is not difficult to understand, it should be a new AppDomain variable, so that it is currently Domain, so reference to Domain on OBJ is equivalent to the current Domain reference.
The key is to execute code in the specified application domain, you need to pay attention to its two ThreadIDs! ! !
Using system;
Public class createAppdomains, PUBLIC CLASS
{
Public static void main ()
{
Appdomain Domaina;
Domaina = appdomain.createdomain ("Mydomaina");
String stringa = "domaina value";
Domaina.SetData ("Domainkey", Stringa;
CommonCallback ();
CrosappdomainDelegate delegatea = new crossappdomainDelegate (CommonCallback);
Domaina.docallback (delegatea);
}
Public static void commoncallback () {
Appdomain Domain;
Domain = appdomain.currentdomain;
Console.writeline ("THE VALUE '" Domain.Getdata ("DomainKey") "' Was Found IN" Domain.FriendlyName.toString () "Running On Thread ID:" Appdomain.getCurrentThreadId (). Tostring ));
}
}
The result of the operation is:
The value '' was found in create_appdomain.exe Running on thread ID: 1372
The value 'Domaina Value' Was Found In Mydomaina Running On Thread Id: 1372
Writing here, suddenly understood the reason why the thread can across multiple Appdomain. First, the main program should be a thread. This should have no problem, and then actually, it has executed a process. In this process, an AppDomain is created after a way to call. Therefore, the thread ID should be the same.
Alternatively, the code is executed in the specified AppDomain, requiring an AppDomain's docallback () method, which requires the example of crossAppdomaIndLegate as a parameter, huh, a bit like a function pointer.