C# 2008 and 2005 Threaded Programming Beginner's Guide Exploit the power of multiple processors for faster, more responsive software. Gastón C. Hillar BIRMINGHAM - MUMBAI C# 2008 and 2005 Threaded Programming Beginner's Guide Copyright © 2009 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: January 2009 Production Reference: 1200109 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-847197-10-8 www.packtpub.com Cover Image by Vinayak Chittar ([email protected]) Credits Author Project Team Leader Gastón C. Hillar Lata Basantani Reviewers Project Coordinator Bogdan Brinzarea-Iamandi Rajashree Hamine Jerry L. Spohn Ron Steckly Project Editorial Manager Abhijeet Deobhakta Senior Acquisition Editor David Barnes Indexer Monica Ajmera Development Editor Shilpa Dube Proofreaders Chris Smith Technical Editor Ron Steckly Rakesh Shejwal Production Coordinator Editorial Team Leader Rajni R. Thorat Akshara Aware Cover Work Copy Editor Rajni R. Thorat Sumathi Sridhar About the Author Gastón C. Hillar has been working with computers since he was eight. He began programming with the legendary Texas TI-99/4A and Commodore 64 home computers in the early 80s. He has a Bachelor degree in Computer Science, graduated with honors, and an MBA (Master in Business Administration), graduated with an outstanding thesis. He has worked as developer, architect, and project manager for many companies in Buenos Aires, Argentina. He was project manager in one of the most important mortgage loan banks in Latin America for several years. Now, he is an independent IT consultant working for several Spanish, German, and Latin American companies, and a freelance author. He is always looking for new adventures around the world. He also works with electronics (he is an electronics technician). He is always researching about new technologies and writing about them. He owns an IT and electronics laboratory with many servers, monitors, and measuring instruments. He is the author of more than 40 books in Spanish about computer science, modern hardware, programming, systems development, software architecture, business applications, balanced scorecard applications, IT project management, Internet, and electronics, published by Editorial HASA and Grupo Noriega Editores. He usually writes articles for leading Spanish magazines "Mundo Linux", "Solo Programadores", and "Resistor". He lives with his wife, Vanesa, and his son, Kevin. When not tinkering with computers, he enjoys developing and playing with wireless virtual reality devices and electronics toys with his father, his son, and his nephew Nico. You can reach him at [email protected] Acknowledgement When writing this book, I was fortunate to work with an excellent team at Packt Publishing Ltd, whose contributions vastly improved the presentation of this book. David Barnes helped me to transform the idea into the final book and to give my first steps working with the Beginner's Guide. Rajashree Hamine made everything easier with her incredible time management. Shilpa Dube helped me realize my vision for this book and provided many sensible suggestions regarding the text, the format, and the flow. The reader will notice her excellent work. Rakesh Shejwal made the sentences, the paragraphs, and the code easier to read and to understand. He has added great value to the final drafts. I would like to thank my technical reviewers Bogdan Brinzarea-Iamandi, Jerry L. Spohn, and Ron Steckly and proofreaders Chris Smith and Ron Steckly, for their thorough reviews and insightful comments. I was able to incorporate some of the knowledge and wisdom they have gained in their many years in the software development industry. The examples and the code include the great feedback provided by Bogdan Brinzarea. Bogdan helped me a lot to include better and shorter code to simplify the learning process. I wish to acknowledge Hector A. Algarra, who always helped me to improve my writing. Special thanks go to my wife, Vanesa S. Olsen, my son Kevin, my nephew, Nicolas, my father, Jose Carlos, who acted as a great sounding board and participated in many hours of technical discussions, my sister, Silvina, who helped me when my grammar was confusing, and my mother Susana. They always supported me during the production of this book. About the Reviewers Bogdan Brinzarea-Iamandi has a strong background in Computer Science holding a Master and Bachelor Degree from the Automatic Control and Computers Faculty of the Politehnica University of Bucharest, Romania, and also an Auditor diploma from the Computer Science department at Ecole Polytechnique, Paris, France. His main interests cover a wide area from embedded programming, distributed and mobile computing, and new web technologies. Currently, he is employed as Supervisor within the team of the Alternative Channels Sector of the IT Division in Banca Romaneasca, a Member of the National Bank of Greece. He is Project Manager for Internet Banking and he coordinates other projects related to new technologies and applications to be implemented in the banking area. Bogdan is also the author of two AJAX books, the popular AJAX and PHP: Building Responsive Web Applications and Microsoft AJAX Library Essentials, also published by Packt. Jerry Spohn is a Manager of Development for a medium-sized software development firm in Exton, Pennsylvania. His responsibilities include managing a team of developers and assisting in architecting a large multi-lingual, multi-currency loan account system, written in COBOL and JAVA. He is also responsible for maintaining and tracking a system-wide program and database documentation web site, in which he uses DotNetNuke as the portal for this information. Jerry is also the owner of Spohn Software LLC., a small consulting firm that helps small businesses in the area with all aspects of maintaining and improving their business processes. This includes helping with the creation and maintenance of web sites, general office productivity issues, and computer purchasing and networking. Spohn Software, as a firm, prefers to teach its clients how to solve their problems internally, rather than acquire a long-term contract, thereby making the business more productive and profitable in the future. Jerry currently works and resides in Pennsylvania, with his wife, Jacqueline, and his two sons, Nicholas and Nolan. Ron Steckly has been developing various platforms for the past several years, recently adopting .NET as his platform of choice. He graduated from U.C. Berkeley with highest distinction in 2004. He recently moved back to sunny Northern California after living for several years in Manhattan. He is currently working as a Web Application Engineer at Empirical Education in Palo Alto, CA and authoring a book on using MySQL with .NET for Packt Publishing. In his spare time, he enjoys studying mathematics (particularly combinatorics), statistics, economics, and new programming languages. I would like to thank my good friends Johannes Castner, Josh Brandt-Young, and David Aaron Engle for all their kindness and patience over the years. To my son, Kevin