Re: Question on AppDomain and Thread
- From: "Dave Sexton" <dave@jwa[remove.this]online.com>
- Date: Sun, 26 Nov 2006 13:42:11 -0500
Hi Leon,
When you create a new AppDomain, objects you created in there using say
AppDomain.CreateInstanctAndUnwrap() run in the same thread as the caller
is
that correct?
When you invoke a method on the object it will be invoked on the current
thread in the AppDomain in which the object was created.
If so, how could I create an AppDomain having its distinct thread? In
particularly, I would like to create several AppDomains each with its own
STA
so that I can have UI object living in them and they are running
concurrently.
AppDomains don't own threads. They are shared amongst all AppDomains in the
process, but a single thread will only execute in one AppDomain at a time.
To execute code concurrently and within a different AppDomain, create a
background thread to invoke an object's method or provide asynchronous
methods on your object (preferred). If the object is a MarshalByRefObject
obtained from a different AppDomain then the code will execute within that
AppDomain, concurrently with the main thread of your application, when
calling on a background thread just as it would an object within the same
AppDomain as the caller.
Kind of like transplanting an unmanaged architecture involving several ole
automation servers (executables with their independent threads) to managed
world using one executables but they are mapped to AppDomain partitioning.
But each AppDomain must have its own thread with its own message pump so
that
WinForm objects can live there.
I'm not sure what value there would be in managing multiple interface
components in multiple AppDomains, but I don't think it's possible anyway.
Controls can only be manipulate on the thread in which they were created.
Therefore, you may be able to have Forms within different AppDomains running
simultaneously, but I doubt that you'll be able to make them children of the
same MDI container, and you won't be able to have controls from different
AppDomains on the same Form. If you were intending to do drag & drop
operations between different Forms, for example, that wouldn't work either
unless the control was recreated in the target AppDomain's thread.
Normally, an application has just one GUI thread and invokes long-running
processes on background threads. If the long-running processes need
isolation from the default AppDomain then you can execute them within a
separate AppDomain, asynchronously, but all GUI components should be created
on the same thread (the one that started the message loop with a call to
Application.Run).
--
Dave Sexton
.
- Follow-Ups:
- Re: Question on AppDomain and Thread
- From: Leon
- Re: Question on AppDomain and Thread
- From: Jon Shemitz
- Re: Question on AppDomain and Thread
- Prev by Date: c# as operator
- Next by Date: Re: c# as operator
- Previous by thread: c# as operator
- Next by thread: Re: Question on AppDomain and Thread
- Index(es):
Relevant Pages
|