473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can i create new thread in the new appdomain ?

in win32 process , when u create new process,u have new main thread.

i know,appDomain r logical procces,that exists in 1 win32 process.

the q:
is there way to create second appDomain (the first 1 is the defualt
appDomain) with his new ("main") thread ?

if not ,what is the easy and clear way to create new thread on the new
appDomain ?
Jul 19 '05 #1
4 6194
[Quick comment - I know *I* would find it easier to read your posts if
you used full English words like "you" and "are" rather than "u" and
"r". I don't know if anyone else agrees with me, admittedly, but
technical newsgroups don't tend to use this style of writing, in my
experience.]

Daylor <Da****@012.net .il> wrote:
in win32 process , when u create new process,u have new main thread.

i know,appDomain r logical procces,that exists in 1 win32 process.
It's not quite that simple. AppDomains are logically separate in terms
of the objects etc, but threads can traverse AppDomains.
the q:
is there way to create second appDomain (the first 1 is the defualt
appDomain) with his new ("main") thread ?
I don't believe .NET has any concept of the "main" thread. There are
foreground threads and background threads - that's all. (I could be
wrong though - and I'm sure COM affects things, with apartments etc.)
if not ,what is the easy and clear way to create new thread on the new
appDomain ?


You don't really create threads "on" an AppDomain. You just create
threads, and they run code.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #2
>
About threads crossing application domains, you need to make a clear distinction between hard threads (OS threads) and soft threads (CLR threads) here.
While hard threads don't have application domain affinity (they are not tied to a specific application domain), CLR threads do have this affinity so they cannot cross AD. borders.

Willy.

The information that I have read indicates that CLR threads are not bounded
to a single appdomain. I agree that there is a distinction between OS
threads and CLR threads. A CLR thread is an object that is mapped to an OS
thread, that the mapping between a CLR thread and an OS thread can change,
and an OS thread can be mapped to different CLR thread objects at different
times. However, this is an implementation detail of the current version of
the CLR and may change in the future (e.g. if the CLR ever implements
threading on top of fibers instead of OS threads). I disagree that CLR
threads are bounded by AD borders.

Here is a direct quote from the documentation of the Thread class:
"GetHashCod e provides identification for managed threads. For the lifetime
of your thread, it will not collide with the value from any other thread,
regardless of the application domain from which you obtain the value.
Note An operating-system ThreadId has no fixed relationship to a managed
thread, because an unmanaged host can control the relationship between
managed and unmanaged threads. Specifically, a sophisticated host can use
the CLR Hosting API to schedule many managed threads against the same
operating system thread, or to move a managed thread between different
operating system threads."

This clearly implies that the thread of execution can cross appdomain
boundaries. Now, it may be that the CLR thread object is bounded in the
sense that because it is not derived from MarshalByRefObj a direct reference
to that object cannot be passed to another appdomain (a copy of the object
such that a new copy that preserves the original identity would be used in
the other appdomain). However, this is not the same thing as the current
thread of execution.

These details are of interest to those of us that like to know what is going
on below the surface; managed code almost never needs to know about this.
From the perspective of an application a sequence of instructions is
executed on a logical thread and the logical thread can cross appdomain
boundaries and preserve its CLR identity.

If my understanding of this is incorrect then please provide more
information.

Dave

Jul 19 '05 #3
Dave wrote:
||| About threads crossing application domains, you need to make a clear
|| distinction between hard threads (OS threads) and soft threads
||| (CLR threads) here.
||| While hard threads don't have application domain affinity (they are
||| not
|| tied to a specific application domain), CLR threads do have
||| this affinity so they cannot cross AD. borders.
|||
||| Willy.
|||
||
||
|| The information that I have read indicates that CLR threads are not
|| bounded to a single appdomain. I agree that there is a distinction
|| between OS threads and CLR threads. A CLR thread is an object that
|| is mapped to an OS thread, that the mapping between a CLR thread and
|| an OS thread can change, and an OS thread can be mapped to different
|| CLR thread objects at different times. However, this is an
|| implementation detail of the current version of the CLR and may
|| change in the future (e.g. if the CLR ever implements threading on
|| top of fibers instead of OS threads). I disagree that CLR threads
|| are bounded by AD borders.
||
|| Here is a direct quote from the documentation of the Thread class:
|| "GetHashCod e provides identification for managed threads. For the
|| lifetime of your thread, it will not collide with the value from any
|| other thread, regardless of the application domain from which you
|| obtain the value. Note An operating-system ThreadId has no fixed
|| relationship to a managed thread, because an unmanaged host can
|| control the relationship between managed and unmanaged threads.
|| Specifically, a sophisticated host can use the CLR Hosting API to
|| schedule many managed threads against the same operating system
|| thread, or to move a managed thread between different operating
|| system threads."
||
|| This clearly implies that the thread of execution can cross appdomain
|| boundaries. Now, it may be that the CLR thread object is bounded in
|| the sense that because it is not derived from MarshalByRefObj a
|| direct reference to that object cannot be passed to another
|| appdomain (a copy of the object such that a new copy that preserves
|| the original identity would be used in the other appdomain).
|| However, this is not the same thing as the current thread of
|| execution.
||
|| These details are of interest to those of us that like to know what
|| is going on below the surface; managed code almost never needs to
|| know about this. From the perspective of an application a sequence
|| of instructions is executed on a logical thread and the logical
|| thread can cross appdomain boundaries and preserve its CLR identity.
||
|| If my understanding of this is incorrect then please provide more
|| information.
||
|| Dave
In order for an class instance to be agile(allowed to flow between AD's) it must not (amongst other rules) refer to instances that
are not agile, this is clearly not the case for the Thread class who refers to managed thread local storage (TLS). However each
managed threads can freely call into a remote application domain and they must do this as quickly as possible therefore they are
treated very carefully by the CLR.
When a managed thread calls into another AD and the target calls Thread.CurrentT hread the CLR will bleed certain Thread properties
like Hashcode, Name, Priority etc. without using a marshaller (MSFT calls this Marshall by bleeding, in fact the target gets a
reference to the original Thread object but access is controlled by the CLR), other properties like the soft thread TLS however
remain AD bound.

Willy.
Jul 19 '05 #4
>
In order for an class instance to be agile(allowed to flow between AD's) it must not (amongst other rules) refer to > instances that are not agile,
this is clearly not the case for the Thread class who refers to managed
thread local storage (TLS).
After I wrote the previous message my memory was tickled by something along
these lines I had read on Chris Brumme's blog. I was incorrect about the
Thread object being marshalled by value; it is indeed marshalled by bleed.
However, my main point remains unchanged. Here's a quote from Chris... "In
other cases, we absolutely require that an instance marshal by bleed.
System.Threadin g.Thread is a good example of this. The same managed thread
can freely call between AppDomains. Since the current marshaler cannot
guarantee that an instance will always bleed, we have made Thread
unmarshalable by the marshaler for now. Then the CLR bleeds it without
using the marshaler when you call Thread.CurrentT hread"
....
"An agile instance must necessarily be of a type we loaded as
domain-neutral. However, the converse is not true. The vast majority of
domain-neutral types do not have agile instances.
If an instance marshals-by-bleed or if it performs identity-preserving
marshal-by-value, then by definition it is agile. The effect is the same in
both cases: it's possible to have direct references to the same instance
from multiple AppDomains"
This contradicts your statement - he states that a thread object is
marshalled-by-bleed and is agile.
However each managed threads can freely call into a remote application domain and they must do this as quickly >as possible therefore they are
treated very carefully by the CLR. When a managed thread calls into
another AD and >the target calls Thread.CurrentT hread the CLR will bleed
certain Thread properties like Hashcode, Name, Priority >etc. without using
a marshaller (MSFT calls this Marshall by bleeding, in fact the target gets
a reference to the >original Thread object but access is controlled by the
CLR), other properties like the soft thread TLS however remain AD bound.


I still don't agree that a managed thread is bounded by an appdomain. I
agree that there are some components of the thread object that are AD bound,
but this does not mean that the thread itself is AD bound, only that some of
the thread's state must be partitioned by appdomain. An example of this are
statics, which can be per-AD, per-thread, per-Context or per-process.
Further, a thread is a repository for a great deal of information that go
far beyond managed properties such as hashcodes and names. The most
important of these is the stack. The stack must record all sorts of things,
such as exception records, appdomain transitions, and execution engine
frames (e.g. ContextTransiti on, Exception, PInvokeCall, etc). A thread may
wander between AppDomains, managed and unmanged code, etc., and all these
transitions are recorded on the stack. The execution engine needs to track
these for purposes such as updating stack-stored object references for the
GC, hold state for security checks, recognize transitions between AD and
managed/unmanaged code, finding handlers for stack unwinding during an
exception, etc.

It may be theoretically possible to build an execution engine that keeps
threads bounded to a single appdomain yet maintains a consistent state
across multiple appdomains, but I don't believe that is the way it was
implemented. Stack-walking would be incredibly inefficient (and error-prone)
if it had to access multiple thread objects. It would be far simpler to have
a single managed thread object that tracked all this information, and then
created objects per-instance, or allowed the CLR to control access to, those
fields that need to remain bounded to an AD.

Dave
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2544
by: Daylor | last post by:
can i do it this way : from the defualt AppDomain : create thread. from the start function of the new thread , create new AppDomain,and then create my class through the AppDomain . this way is ok ? or there is other way to do that ?
7
522
by: Daylor | last post by:
in win32 process , when u create new process,u have new main thread. i know,appDomain r logical procces,that exists in 1 win32 process. the q: is there way to create second appDomain (the first 1 is the defualt appDomain) with his new ("main") thread ? if not ,what is the easy and clear way to create new thread on the new appDomain ?
3
2883
by: Keyee Hsu | last post by:
Hi, I have a C# app that creates an AppDomain, enters it, and spawns an asyn thread to do some work and then block itself. Upon the completion of the work, the async thread supposedly terminates, then the original thread unblocks, unloads the AppDomain, and starts the whole process all over again. I get the System.AppDomainUnloadedException saying that the AppDomain from which the async thread resides has been unloaded. Now, if I were...
20
3010
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
5
6980
by: Nak | last post by:
Hi there, I have a very simple console application that I'm trying to handle the thread exception on, as I understand it there is no Application object available to a console application, so I can't use this. Instead you need to use "Thread.GetAppDomain.UnhandledException", this "works" to a certain extent but from the look if it you merely get added to a chain of event handlers, and the runtime is still above you, so I can't use this...
2
2207
by: Artem | last post by:
When I use the method Thread.Abort, it only sends a request of aborting to OS to stop a thread. The thread itself isn't killed and allocated resources aren't released. I tried to run that thread inside of a domain, specially created for it, but the domain can't be unloaded, 'till there are some not- stopped threads. Please, tell me are there any ways to immediately stop a thread or unload a domain? Thanks beforehand!
4
5337
by: illegal.prime | last post by:
Hi all, I'm getting unexpected results when trying to preload assemblies into an AppDomain I'm creating. Upon creation of the AppDomain - I attach an AssemblyResolve to both my current AppDomain and the new AppDomain I create. I copy all the assemblies/dlls into a new directory and then try loading them all into the new AppDomain using the following: private void LoadAssembliesFromDirectory(AppDomain appDomain, string directory)
0
1859
by: jeremyje | last post by:
I would like to create an application where I have many concurrent processes being managed by a monitoring process. Each process that is "managed" will be invoked from an assembly dll (think reflection). I want a way to invoke these processes in parallel utilizing multi-core processors but I'd like to have the protection that AppDomains provide. I was doing some research where I found that there is no true isolation between threads and...
1
7190
by: Polaris | last post by:
Hi C# Experts: I have 2 threads and I like to run them in seperate AppDomains. In other words, I want to start 2 new AppDomains and then run a thread within each of the AppDomains. How to do it? Thanks Polaris
0
8372
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8814
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.