Online Mind Mapping and Brainstorming

Create your own awesome maps

Online Mind Mapping and Brainstorming

Even on the go

with our free apps for iPhone, iPad and Android

Get Started

Already have an account? Log In

Chapter 7:Threading by Mind Map: Chapter 7:Threading
1.0 stars - 1 reviews range from 0 to 5

Chapter 7:Threading

  Chapter Summary   The Thread class can be used to create multiple paths for simultaneous execution in your own applications. ■ Using the lock (SyncLock in Visual Basic) keyword will allow you to write threadsafe access to your code’s data. ■ You must be careful in writing thread-safe code to avoid deadlock situations. ■ The ReaderWriterLock class can be used to write thread-safe code that is less prone to allowing only a single thread at a time to access its data. ■ The WaitHandle derived classes (Mutex, Semaphore, and Event classes) exemplify Windows operating-system-level synchronization objects. ■ Much of the .NET Framework supports the Asynchronous Programming Model (APM) to allow for asynchronous execution of code without having to directly deal with the ThreadPool or Threads. ■ The ThreadPool is a convenient class that enables fast creation of threads for queuing up code to run as well as for waiting for WaitHandle derived objects. ■ Timers are useful objects for firing off code on separate threads at specific intervals

Lession 1:Creating Threads

Simple Threads

Thread Properties IsThreadPoolThread Gets whether this thread is a thread in the thread pool. Name Gets or sets a name associated with the thread. ThreadState Gets the ThreadState value for the thread.   Static Thread Properties CurrentContext Gets the current ThreadContext object related to the current thread CurrentPrincipal Gets and sets the user associated with the current thread CurrentThread Gets the current running thread   Static Thread Methods BeginCriticalRegion Used to notify the host that code to be executed cannot be aborted safely. Aborting a thread between a BeginCriticalRegion and EndCriticalRegion might leave an AppDomain in an unsafe state.   EndCriticalRegion Used to notify the host that you have reached the end of a critical region Sleep Blocks the current thread for a certain amount of time. Relinquishes execution to other threads to allow them to do work. ThreadState Enumeration   Aborted The thread has stopped.   AbortRequested The thread has been requested to abort, but the abort is still pending and has not received the ThreadAbortException. Background The thread is running as a background threa Running The thread has started Stopped The thread has stopped. StopRequested The thread is being requested to stop. For internal use only.  

Passing Data to threads

In most real-world use of threading, you will need to pass information to individual threads. To do this, you need to use a new delegate called Parameterized- StartThread.   This delegate specifies a method signature with a single parameter of type Object and returns nothing     // C# ParameterizedThreadStart operation = new ParameterizedThreadStart(WorkWithParameter); // Creates, but does not start, a new thread Thread theThread = new Thread(operation); // Starts the work on a new thread theThread.Start("Hello"); // A Second Thread with a different parameter Thread newThread = new Thread(operation); newThread.Start("Goodbye");

Stopping Threads

Controlling threads in your applications often requires that you be able to stop threads. The primary mechanism for stopping threads is to use the Thread.Abort method. When the Thread.Abort method is called, the threading system prepares to throw a Thread- AbortException in the thread. Whether the exception is caught or not, the thread is stopped after it is thrown. BeginCriticalRegion and EndCriticalRegion. We can add calls to these methods to tell the threading system that it can abort this thread, just not within this critical region // Setting data Thread.BeginCriticalRegion(); SomeClass.IsValid = true; SomeClass.IsComplete = true; Thread.EndCriticalRegion();

Execution Context

  The ExecutionContext class provides a single container for all information relevant to a logical thread of execution. This includes security context, call context, synchronization context, localization context, and transaction context.     For more information:

Lession 2: Sharing Data

Once you start to work with multiple threads in an application, you become responsible for the protection of any shared data that can be accessed from multiple thread

Avoiding Collision

Before threading became available, you could expect that any access of data was being done by one object at a time. Now that we have entered the multithreaded world, we have to deal with the fact that multiple threads might be interrogating our objects simultaneously. This causes problems in some deceptive ways.   You can use the Interlocked class to perform the incrementing operation Interlocked Static Methods Add Adds two integers as an atomic operation. Can be used for subtraction by adding a negative integer. Decrement Subtracts one from a value as an atomic operation. Exchange Swaps two values as an atomic operation.     Synchronization Locks The purpose of synchronization locks is to allow you to synchronize access to objects in .NET    

Synchronization Locks

Lession 3:The Asynchronous Programming Model

Understanding Asynchronous Programming(APM)

Using the ThreadPool

Using Timer Objects