tailieunhanh - Chapter 11 - Threading in C#

“This chapter covers the tasks required in creating multithreaded applications in the C# managed virtual execution environment. If you’re familiar with threading in the native Win32 environment, you’ll notice the significant differences. Moreover, the managed environment provides much more infrastructure for making the job easier. This chapter covers the various synchronization facilities available to your applications.” | Chapter 11. Threading in C# Hoang Anh Viet VietHA@ HaNoi University of Technology 3. Data Types 2008 © 2008 Microsoft Objectives “This chapter covers the tasks required in creating multithreaded applications in the C# managed virtual execution environment. If you’re familiar with threading in the native Win32 environment, you’ll notice the significant differences. Moreover, the managed environment provides much more infrastructure for making the job easier. This chapter covers the various synchronization facilities available to your applications.” 3. Data Types 2008 © 2008 Microsoft Roadmap Threading in C# and .NET Synchronizing Work Between Threads Using ThreadPool 3. Data Types 2008 © 2008 Microsoft Threading in C# and .NET By default, a C# program has one thread. This thread executes the code in the program starting and ending with the Main method. This thread terminates when Main returns. However, auxiliary threads | Chapter 11. Threading in C# Hoang Anh Viet VietHA@ HaNoi University of Technology 3. Data Types 2008 © 2008 Microsoft Objectives “This chapter covers the tasks required in creating multithreaded applications in the C# managed virtual execution environment. If you’re familiar with threading in the native Win32 environment, you’ll notice the significant differences. Moreover, the managed environment provides much more infrastructure for making the job easier. This chapter covers the various synchronization facilities available to your applications.” 3. Data Types 2008 © 2008 Microsoft Roadmap Threading in C# and .NET Synchronizing Work Between Threads Using ThreadPool 3. Data Types 2008 © 2008 Microsoft Threading in C# and .NET By default, a C# program has one thread. This thread executes the code in the program starting and ending with the Main method. This thread terminates when Main returns. However, auxiliary threads can be created and used to execute code in parallel with the primary thread. These threads are often called worker threads. 3. Data Types 2008 © 2008 Microsoft Threading in C# and .NET Worker threads can be used to perform the following without tying up the primary thread. Time-consuming tasks. Or time critical tasks. For example, worker threads are often used In server applications to fulfill incoming requests without waiting for the previous request to be completed. To perform "background" tasks in desktop applications so that the main thread--which drives user interface elements--remains responsive to user actions. Multithreading Multithreading can introduce resource-sharing issues Deadlocks And race conditions Multiple threads are best for tasks that require different resources. File handles Network connections Assigning multiple threads to a single resource causes Synchronization issues Threads frequently are blocked. States of a Thread Create and Terminate .