Developer Resources



How to: Create an Application Domain 

A common language run-time host creates application domains automatically when they are needed. However, you can create your own application domains and load into them those assemblies that you want to manage personally. You can also create application domains from which you execute code.

You create a new application domain using one of the overloaded Create Domain methods in the System.AppDomain class. You can give the application domain a name and reference it by that name.

EXAMPLE HERE


You are an application developer for a company. You are creating a business logic component that requires long calculations.

You identify several tasks within the application that can be done asynchronously. These tasks are interdependent and require complex synchronization techniques to manage efficiently. You decide to take advantage of the new thread management features in Microsoft .NET 2.0.

Which code should you use to create and start the application threads?

  1. ThreadPool.QueueUserWorkItem( Task1 );
    ThreadPool.QueueUserWorkItem( Task2 );
    ThreadPool.QueueUserWorkItem( Task3 );

  2. Thread th1 = new Thread( Task1 );
    Thread th2 = new Thread( Task2 );
    Thread th3 = new Thread( Task3 );
    th1.Start();
    th2.Start();
    th3.Start();

  3. ThreadPool thPool = new ThreadPool("Current Application");
    thPool.QueueUserWorkItem( Task1 );
    thPool.QueueUserWorkItem( Task2 );
    thPool.QueueUserWorkItem( Task3 );

  4. ThreadPool thPool = new ThreadPool("Current Application");
    Thread th1 = new Thread( Task1 );
    Thread th2 = new Thread( Task2 );
    Thread th3 = new Thread( Task3 );
    th1.StartInPool(thPool);
    th2.StartInPool(thPool);
    th3.StartInPool(thPool);

Answer:

  1. ThreadPool.QueueUserWorkItem( Task1 );
    ThreadPool.QueueUserWorkItem( Task2 );
    ThreadPool.QueueUserWorkItem( Task3 );

Tutorial:
You should use the following code to create and start the application threads:

ThreadPool.QueueUserWorkItem( Task1 );
ThreadPool.QueueUserWorkItem( Task2 );
ThreadPool.QueueUserWorkItem( Task3 );

This code uses the QueueUserWorkItem method of the ThreadPool class to add tasks to the current application domain's thread pool. The QueueUserWorkItem method takes a WaitCallback delegate as an argument and manages the tasks using background threads. This allows the developer to concentrate on business logic and requires minimal synchronization code.

You should not use either of the code fragments that instantiate the Thread objects explicitly. This will require excessive synchronization code to manage effectively. In this scenario, you should use the ThreadPool class to minimize the synchronization code.

You should not use the code that instantiates a ThreadPool object because the ThreadPool class is a static class and cannot be instantiated. The methods of the ThreadPool class are all class members and do not require an instance to invoke. In this scenario, you should invoke the QueueUserWorkItem method on the ThreadPool class directly.

Reference: certmag