C# Threads: Using Thread TypeThese C# articles show how to implement threads in programs. They cover the Thread type in System.Threading.
Threads improve performance. They move computations to a separate logical processor. They are handled in the .NET Framework with classes from the base class library. Multithreading, even with these classes, is difficult.
How can you use the Thread type in a C# program? This console program creates two instances of the Thread type and uses the ThreadStart class to specify their target methods A and B.ThreadStart
Next: The threads are started and then they are joined. The two threads each run for a certain number of milliseconds.Join
Thread A: This thread starts first. It terminates after 100 milliseconds, a tenth of a second.
Thread B: This thread starts second. It ends after 1 second has elapsed. It prints B to the Console.
C# program that uses threading constructs
static void Main()
Thread thread1 = new Thread(new ThreadStart(A));
Thread thread2 = new Thread(new ThreadStart(B));
static void A()
static void B()
A good way to implement simple threading in Windows Forms programs is to use the BackgroundWorker class. This results in error-free and reliable threading. The class lacks some features you can implement by directly using threads.BackgroundWorker
With the ThreadPool, you can execute many different threads in parallel and have the system recycle them as soon as possible. This is an ideal way to start many short-lived jobs. The ThreadPool type provides built-in functionality.ThreadPoolThreadPool.SetMinThreads
We show how to use the lock statement in the C# language. It also relates the Theory of Relativity to concurrent programs. The lock statement is a simplified syntax for Monitor method calls.Lock
Volatile: The volatile modifier ensures a field is used in a thread-safe way. This modifier can eliminate the need for lock statements.volatile
Interlocked: The Interlocked type provides a way to change or compare a value in an atomic way.Interlocked
Continuing on, the .NET Framework provides the Mutex type in the System.Threading namespace. This type is used to synchronize processes and threads within a single process. We use Mutex to create a single-instance program.Mutex
Tip: Mutex can be used in other program contexts, but generally the lock-keyword and the async and await keywords are clearer.
By using the Sleep method, you can pause a thread and not have it incur any actual CPU usage. Instead, the thread is simply delayed for a certain amount of time, measured in milliseconds.Thread.Sleep
SpinWait: With Thread.SpinWait, your computer can wait too, doing computationally intensive loops. This is a long-running loop.SpinWait
The .NET Framework 4.5 added the async and await keywords. Async modifies a method. It indicates a method can be asynchronously run. The await keyword waits for an async method to return. It is used within an async method.async, await
Tip: Async and await make some threading patterns easier to implement. But they require planning and a careful design.
A process is separate at the level of the operating system. It can contain multiple threads. We can use separate processes instead of separate threads. This can move some of the complexity to the operating system.Process
Summary. Multiple threads can be executed in one process. As an alternative to using multiple threads, you can use multiple processes. Please also check out the Process type. Threading introduces extra complexity into programs.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.