using application domain does not work as expected

T

Tony Johansson

Hello!

Here is a program that is using Application Domain but it doesn't work as
expected.
Method SomeMethod() is executing in a separate thead in a separate
application Domain.that is called SecondDomain.
In this method SomeMethod() I divide by zero on purpose so I expect that
this domain should stop and the main thread should continue with the for
loop.
What happens now is that I get an exception which is correct about division
by with zero and then the whole application stop.
So the main thread doesn't continue with the for loop.

My purpose of this test was to check that the main thread in the primary
application domain could continue executing when
the new application domain with it's own additional thread run into problem
such as divide by zero.

using System;
using System.Text;
using System.Threading;

public class Program
{
static void Main(string[] args)
{
AppDomain domain = AppDomain.CreateDomain("SecondDomain");
Thread.CurrentThread.Name = "Thread 2";

//// Thread.Start in first domain
Thread thread = new Thread(SomeMethod);
thread.Name = "Thread 1";
thread.Start();

//// Current thread, for simpliicty, second domain
////The CrossAppDomainDelegate is a good tool for threading across
appdomain
////boundaries.
domain.DoCallBack(new CrossAppDomainDelegate(SomeMethod));

for (int i = 0; i < 10; i++)
{
Console.WriteLine("In main Thread=" + i);
Thread.Sleep(1000);
}

Console.ReadLine();
}

static void SomeMethod()
{
int tal = 10;
int sum = 10;

Console.WriteLine("Domain: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Thread: " +
System.Threading.Thread.CurrentThread.Name);
Thread.Sleep(2000);
tal = 0;
sum = sum / tal;
}
}

//Tony
 
T

Tony Johansson

Peter Duniho said:
Hello!

Here is a program that is using Application Domain but it doesn't work as
expected.
Method SomeMethod() is executing in a separate thead in a separate
application Domain.that is called SecondDomain.
In this method SomeMethod() I divide by zero on purpose so I expect that
this domain should stop and the main thread should continue with the for
loop. [...]

You execute the code in the second domain in your original thread.

Perhaps you could explain why it is you a) think that the new thread is
executing in the new AppDomain (it's not), and b) why you think that an
exception in the new AppDomain won't propagate back up to the caller in
the original AppDomain, thus stopping the application.

Pete

But as I have read somewhere it's one of the advantages of using multiple
application domains within the same process is
that if code running in one application domain will not cause problem in the
other application domain.
Do you mean that this is wrong ?

//Tony
 
T

Tony Johansson

Peter Duniho said:
But as I have read somewhere it's one of the advantages of using multiple
application domains within the same process is
that if code running in one application domain will not cause problem in
the
other application domain.
Do you mean that this is wrong ?

Taken literally, yes.it's wrong that "code running in one application will
not cause [any] problem in the other application domain".

AppDomains provide a specific kind of isolation (i.e. for your data). Code
executing in one AppDomain cannot read or modify data owned by another
AppDomain. But it doesn't isolate _all_ potential problems.

You still need to catch exceptions if you don't want them to take down the
process.

Pete

You say that
"Perhaps you could explain why it is you a) think that the new thread is
executing in the new AppDomain (it's not), "
but that is strange because when I run the code the method SomeMethod
say that the Domain is SecondDomain which is the new AppDomainthat is
created

So I mean because the method SomeMethod say that appDomian is
SecondDomain I have hard to understand that this is wrong.


One more question I tested this code and here as you can see I start to
execute another assembly which is called DomainTest
from process AssemblyA(process) in method SomeMethod and this assembly
DomainTest is dividing with zero on purpose and this cause the whole process
AssemblyA to stop.

So it seems to me that it doesn't matter if you start to execute a new
assembly in a new App domain and this new Assembly
throw an execption it will stop the parent process.
I'm I right ?

using System;
using System.Text;
using System.Threading;

public class Program
{
static void Main(string[] args)
{
AppDomain domain = AppDomain.CreateDomain("SecondDomain");
Thread.CurrentThread.Name = "Thread 2";

//// Thread.Start in first domain
Thread thread = new Thread(SomeMethod);
thread.Name = "Thread 1";
thread.Start();

//// Current thread, for simpliicty, second domain
////The CrossAppDomainDelegate is a good tool for threading across
appdomain
////boundaries.
domain.DoCallBack(new CrossAppDomainDelegate(SomeMethod));

for (int i = 0; i < 10; i++)
{
Console.WriteLine("In main Thread=" + i);
Thread.Sleep(1000);
}
Console.ReadLine();
}

static void SomeMethod()
{
Console.WriteLine("Domain: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Thread: " +
System.Threading.Thread.CurrentThread.Name);

AppDomain.CurrentDomain.ExecuteAssembly("DomainTest.exe");
Thread.Sleep(2000);
}
}

//Tony
 
T

Tony Johansson

Peter Duniho said:
But as I have read somewhere it's one of the advantages of using multiple
application domains within the same process is
that if code running in one application domain will not cause problem in
the
other application domain.
Do you mean that this is wrong ?

Taken literally, yes.it's wrong that "code running in one application will
not cause [any] problem in the other application domain".

AppDomains provide a specific kind of isolation (i.e. for your data). Code
executing in one AppDomain cannot read or modify data owned by another
AppDomain. But it doesn't isolate _all_ potential problems.

You still need to catch exceptions if you don't want them to take down the
process.

Pete

You say that
"Perhaps you could explain why it is you a) think that the new thread is
executing in the new AppDomain (it's not), "
but that is strange because when I run the code the method SomeMethod
say that the Domain is SecondDomain which is the new AppDomainthat is
created

So I mean because the method SomeMethod say that appDomian is
SecondDomain I have hard to understand that this is wrong.


One more question I tested this code below and here as you can see I start
to
execute another assembly which is called DomainTest
from process AssemblyA(process) in method SomeMethod and this assembly
DomainTest is dividing with zero on purpose and this cause the whole process
AssemblyA to stop.

So it seems to me that it doesn't matter if you start to execute a new
assembly in a new App domain and this new Assembly
throw an execption it will stop the parent process.
I'm I right ?

using System;
using System.Text;
using System.Threading;

public class Program
{
static void Main(string[] args)
{
AppDomain domain = AppDomain.CreateDomain("SecondDomain");
Thread.CurrentThread.Name = "Thread 2";

//// Thread.Start in first domain
Thread thread = new Thread(SomeMethod);
thread.Name = "Thread 1";
thread.Start();

//// Current thread, for simpliicty, second domain
////The CrossAppDomainDelegate is a good tool for threading across
appdomain
////boundaries.
domain.DoCallBack(new CrossAppDomainDelegate(SomeMethod));

for (int i = 0; i < 10; i++)
{
Console.WriteLine("In main Thread=" + i);
Thread.Sleep(1000);
}
Console.ReadLine();
}

static void SomeMethod()
{
Console.WriteLine("Domain: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Thread: " +
System.Threading.Thread.CurrentThread.Name);

AppDomain.CurrentDomain.ExecuteAssembly("DomainTest.exe");
Thread.Sleep(2000);
}
}

//Tony
 

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