The C# threading model - Part 1

Posted by in C# category on for Beginner level | Points: 250 | Views : 11783 red flag

We will see The C# threading model.


Threading is the process C# uses to support multitasking within applications. Each thread is a unit of processing that carries out a particular function, and the system processor divides its time between the active threads.

The rapid switching between threads makes it appear that several threads are operating at the same time. This can greatly increase efficiency, since the application doesn't have to wait for one process to finish before another process can start. Each process must be synchronized to ensure the safety of each thread.

Successful threading depends on dividing the processor time between competing threads, and there are two ways of handling this:

Cooperative threading 
Pre-emptive threading 

Cooperative Threading 

In cooperative threading, each thread is responsible for making the processor available to the other threads. However, this means that all threads are vulnerable if one thread encounters a problem or is badly coded. 

Pre-emptive Threading 

In the pre-emptive threading model, multitasking is managed by the processor itself. Processor time is divided up into timeslices, units of a few thousandths of a second each, and at the end of each timeslice, control is switched between running threads. 

The use of threading may not be the right choice for all applications, and sometimes it can slow them down.

Threading should be used only when you need increased concurrency, better utilization of CPU time, or simplification of the design of complex applications that use queues and asynchronous processing.

Visual Basic 6 (VB6) includes limited support for multithreading applications.

All components created with VB6 use the apartment model, whether they're single threaded or multithreaded.

The .NET Framework does not use apartments. The free-threading model that is standard in C# can be created in VB6 only with the support of Java or VC++.

C# natively supports thread synchronization with the lock statement. Threading is implemented in C# by using delegates to attach methods to new threads.

When a new thread is created, it takes a delegate as an argument. The delegated method provides the functionality for the thread.

The logical process in which a thread runs is called the AppDomain. This isolates the thread so that a thread failure will not bring down the rest of the program. It also allows several threads to run in the same physical process.

We will see in more details with few examples in my coming articles.....

Thanks and Have Fun!!!!! 

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: John Doe
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/22/2011 7:38:35 AM
Country: India

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)