How to start a thread in a new AppDomain ?

P

Polaris

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
 
D

Dave

This bit of code should point you in the right direction.

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

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

// Current thread, for simpliicty, second domain
domain.DoCallBack(new CrossAppDomainDelegate(SomeMethod));

Console.ReadLine();
}

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

}
}

The CrossAppDomainDelegate is a good tool for threading across appdomain
boundaries.

HTH,
D. Swicegood
 

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