Threading in .NET 2.0

If you have used threading in .NET framework 1.x, you may be familiar with Suspend and Resume methods. Please note that these two methods have been deprecated in .NET Framework 2.0. As a replacement, use one of the following thread synchronization methods based on the scenario.


1. Use the Interlocked class and associated Add, Increment, Decrement methods if the operation that you want to perform is a very simple mathematical operation.
2. Use lock object to encapsulate the critical section (the lines of code that need to run as one atomic operation).
3. Use Monitor class for more granular control. This class exposes methods such as TryEnter and Wait in order to obtain an exclusive lock.
4. Use ReaderWriterLock class in scenarios where multiple threads need to read from a common source and only one thread needs to write to the common source at any given point of time.
5. For thread synchronization across AppDomains or processes, you can use kernel-level objects exposed through .NET classes such as Mutex, Semaphore and EventWaitHandle.

No comments:

Followers

Powered by Blogger.