Chapter 19. Threading
C# allows you to execute code in parallel through multithreading.
A thread is analogous to the operating system process in which your application runs. Just as processes run in parallel on a computer, threads run in parallel within a single process. Processes are fully isolated from each other; threads have just a limited degree of isolation. In particular, threads share (heap) memory with other threads running in the same application domain. This, in part, is why threading is useful: one thread can fetch data in the background while another thread displays the data as it arrives.
This chapter describes the language and Framework features for
creating, configuring, and communicating with threads, and how to
coordinate their actions through locking and signaling. It also covers the
predefined types that assist threading: BackgroundWorker
, ReaderWriterLock
, and the Timer
classes.
Threading’s Uses and Misuses
A common use for multithreading is to maintain a responsive user interface while a time-consuming task executes. If the time-consuming task runs on a parallel “worker” thread, the main thread is free to continue processing keyboard and mouse events.
Whether or not a user interface is involved, multithreading can be useful when awaiting a response from another computer or piece of hardware. If a worker thread performs the task, the instigator is immediately free to do other things, taking advantage of the otherwise unburdened computer.
Another use for multithreading ...
Get C# 3.0 in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.