Dynamic AppDomains

G

Guest

I have an application that uses the FileSystemWatcher to monitor for the
"drop" of certain files in a folder. Custom actions are then executed based
on the file name, size, etc.

This application can listen for an unlimited number of files and therefore,
runs an unlimitede number of threads that implement the FileSystemWatcher.

However, if a critical error occurs one of these threads, the whole app
crashes.

Is there a way I can create an AppDomain (therefore, another process) for
each file listener? I guess I am trying to simulate what IIS6 does when you
add an applications to different ApplicationPools so that the crash of one
will not affect the other.

Can anyone point me in the right place? Thanks.

Shaun
 
G

Guest

Hi,

There might be a number of things you need to look at with your program such
as using a try..catch block to trap the exception (if possible) and handle
it accordingly. The other is to find out why the exception is occurring as
this might help resolve the issues i.e. it could be because the file is
still in the process of been written etc. But of course exceptions do occur
in exceptional circumstances. The other is the use of unlimited threads.
When you create a thread you are allocated a segment of time slice of your
main application not of the operating system (OS). Having multi threads does
help application performance some times considerable. However, if you have
to many threads this can start hurting performance as the OS has to swap out
registers, stacks etc. and set-up the environment for the next process
thread to execute. This takes time and at some point the scales swap when
the time gained by the thread is lost by the swap-out and limited time the
thread has to run. The way this could be resolved is to crate a limited
number of threads in a thread pool. When all threads are allocated queue up
the next requests until a thread is release back to the pool.

To resolve the unexpected exception killing the entire application you can
create a new app dominion and execute the threads in that. If you create a
thread manager in the new AppDominion that creates a limited number of
threads. Then to increase the number of threads (if required) create a new
app dominon. This means if a thread has to be killed it will only affect a
limited number of threads.

So how do you do this? First you need to create the temporary AppDomain

AddDomain aDomain = AppDomain.CreateDomain("myTmpDom");

If you're going to create a number of these generate a unique name
for each one.

Second you need to load the Assembly (exe/dll) into the new AppDomain

ADomain.Load("FileSystemWatcher.dll")

This will load an Assembly called FileSystemWatcher.dll (or whatever you
choose to call it), which would contain your thread pool manager and
threads. If something does go wrong you then only need to kill the temporary
domain leaving the other temp domains (if created) and your primary
Application running.

- Mike
 
G

Guest

Before I forget you might want to create and control a specific object in
the new AppDomain here is the method for that.

To create an instance of the object that will run in the AppDomain. This is
achieved by calling CreateInstance which has 3 overloads.

ObjectHandle hndPoolManager = aDomain.CreateInstance("myTmpDom",
"myNameSpace.PoolManager");

Where the second param must be the fully-qualified namespace of the
object. Alternatively you can call typeOf(PoolManager).ToString() to get it.

If you require to interface with the object to call methods or pass data
back and forth you will need to create a proxy to communicate with the
object as it's not in your current domain. This uses Remoting to marshal the
data between the domains. Fortunately the .Net Framework provides a method
to dynamically create this for you.

(PoolManager)hndPoolManager.Unwrap();

Alternativly you can accomplish these two methods by just doing the
following:

(PoolManager)aDomain.CreateInstanceAndUnwrap(.., ..);

- Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top