Category: Threading in C Sharp

Threading in C Sharp

Multithreaded Timers

Multithreaded Timers

System.Threading.Timer is the simplest multithreaded timer: it has just a constructor and two methods. In the following example, a timer calls the Tick method, which writes “tick…” after five seconds have elapsed, and then...

Timers

Timers

If you need to execute some method repeatedly at regular intervals, the easiest way is with a timer. Timers are convenient and efficient in their use of memory and resources—compared with techniques such as...

Parallel Class

Parallel Class

Another abstraction of threads that is new with .NET 4 is the Parallel class. This class defi nes static methods for a parallel for and foreach . With the language defi ned for for...

Synchronization in Threads

Synchronization in Threads

When we have multiple threads that share data, we need to provide synchronized access to the data. We have to deal with synchronization issues related to concurrent access to variables and objects accessible by...

Thread Pooling

Thread Pooling

Whenever you start a thread, a few hundred microseconds are spent organizing such things as a fresh private local variable stack. Each thread also consumes (by default) around 1 MB of memory. The thread...

Thread Priority

Thread Priority

A thread’s Priority property determines how much execution time it gets relative to other active threads in the operating system, on the following scale: The following code example shows the result of changing the...

Foreground and Background Threads

Foreground and Background Threads

A managed thread is either a background thread or a foreground thread. Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once...

Sharing Data between Threads

Sharing Data between Threads

If threads do want to share data, they do so via a common reference. This can be a captured variable as we saw previously—but much more often it’s a field. Here’s an example: Example...

Passing Data to a Thread

Passing Data to a Thread

The easiest way to pass arguments to a thread’s target method is to execute a lambda expression that calls the method with the desired arguments: Example Another technique is to pass an argument into...

isAlive and join()

isAlive and join()

Once started, a thread’s IsAlive property returns true, until the point where the thread ends. A thread ends when the delegate passed to the Thread’s constructor finishes executing. Once ended, a thread cannot restart....

Creating Threads

Creating Threads

A client program (Console, WPF, or Windows Forms) starts in a single thread that’s created automatically by the CLR and operating system (the “main” thread). Here it lives out its life as a single-threaded...

Threading Introduction

Threading Introduction

A thread is an independent stream of instructions in a program. All your C# programs up to this point have one entry point — the Main() method. Execution starts with the fi rst statement...