Search⌘ K
AI Features

Multithreading Overview

Explore the fundamentals of multithreading in C# and .NET to understand how multiple threads enable simultaneous task execution. Learn about thread properties, context switching, and how threads operate on single and multicore processors to keep applications responsive.

Modern applications perform many tasks at the same time. For instance, when we send a photo to our family members with a messenger application, we continue receiving messages while our photo is being uploaded. The methods handling the photo upload and incoming messages run almost simultaneously. Different parts of the system continue to operate at the same time because they run on separate threads. Utilizing several threads is called multithreading.

Note: Threads are unique execution paths with separate control flows. That is, threads don’t affect one another unless they use a shared resource, like memory or file storage.

All code examples we’ve written so far run on a single thread. The subsequent line of code doesn’t run until the previous one finishes executing. In a real-life application, it would look something like this:

  • We click the “Download” button and the download starts. The user interface freezes until the download finishes.

Let’s introduce multithreading to this scenario:

  • We click the “Download” button and the download starts on a background thread. The user interface stays responsive and doesn’t freeze. ... ...