Implementing service processes, threading and application domains in a .NET Framework application

These questions are based on 70-622CSHP – TS: Microsoft .NET Framework 2.0 – Application Development Foundation (C#.NET) Microsoft Self-Test Software Practice Test.


Objective: Implementing service processes, threading and application domains in a .NET Framework application
SubObjective: Develop multithreaded .NET Framework applications (Refer System.Threading namespace)


Item No. 70-536CSHP.2.2.4
Single Answer, Multiple Choice


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:
MSDN2 Library > Development Tools and Languages > Visual Studio > .NET Framework Programming in Visual Studio > .NET Framework Advanced Development > Threading > Threading Objects and Features > The Managed Thread Pool


MSDN2 Library > .NET Development > .NET Framework SDK > Class Library Reference > System.Threading > ThreadPool Class

Like what you see? Share it.Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone
cmadmin

ABOUT THE AUTHOR

Posted in Archive|

Comment:

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>