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
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.
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.....