Chapter 13. Threading

This chapter looks at the support that C# and the .NET base classes offer for developing applications that employ the use of multiple threads. It briefly examines the Thread and the ThreadPool classes, through which much of the threading support takes place, and you develop a couple of examples that illustrate threading principles. Then you examine some of the issues that arise with thread synchronization. Due to the complexity of the subject, the emphasis of this chapter is solely on understanding some of the basic principles involved through some simple sample applications. This chapter focuses on

  • How to start a thread

  • Providing thread priorities

  • Controlling access to objects through synchronization

By the end of the chapter, you will feel quite comfortable working with threads in your code. The following section starts by running through the basics of threading.


A thread is an independent stream of instructions in a program. All your C# programs up to this point have one entry point—the Main() method. Execution starts with the first statement in the Main() method and continues until that method returns.

This program structure is all very well for programs in which there is one identifiable sequence of tasks, but often a program actually needs to be doing more than one thing at the same time; for example, when you start up Internet Explorer and get increasingly frustrated with the time it takes a page to load. Eventually, you get so fed up (if you're like ...

Get Professional C# 2005 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.