Every process in the operating system begins executing code with a single thread. The OS does not really see processes when it is time to run code—it sees threads. When the operating system starts a process (an executable), the code for that executable begins with a thread. The first thread (the one that every process gets when it is launched) is called the main thread. A process continues to live as long as the main thread lives. The OS assigns each thread a unique thread ID. Any process can reach a thread (if it has the right permissions) by using its ThreadId. In the same fashion, the OS also assigns each process a process ID, or PID.

Every thread defines an execution sequence. The OS demands that each thread be attached to a procedure called the thread procedure, or ThreadProc. The main subroutines’s thread procedure is main in console applications or winmain in Windows applications. In Visual Basic, the code for the winmain procedure executes the VB procedure Sub Main, if there is one. In fact, as you will see later, Sub Main is a procedure that Visual Basic runs for every thread that it launches.

Threads are kernel objects, which means that the code in kernel.dll is responsible for creating, destroying, and managing threads for the system.

The OS does not wait for each thread to be done with its task. The OS has a scheduler that gives each thread a certain amount of time to run based on the thread priority. Each thread receives a portion of time, known as a quanta ...

