Article ID: 129797 - View products that this article applies to.
This article was previously published under Q129797
A Windows application can consist of more than one process, and a process can consist of more than one thread. The Microsoft Win32 application program interface (API) supports multitasking, which creates the effect of simultaneous execution of multiple processes and threads. This article describes processes and threads, and explains how to create and use them from Microsoft Visual Basic, with a step-by-step example.
A process is a program that is loaded into memory and prepared for execution. Each process has a private virtual address space. A process consists of the code, data, and other system resources such as files, pipes, and synchronization objects that are accessible to the threads of the process. Each process is started with a single thread, but additional independently executing threads can be created.
A thread can execute any part of the program's code, including a part executed by another thread. Threads are the basic entity to which the operating system allocates CPU time. Each thread maintains a set of structures for saving its context while waiting to be scheduled for processing time. The context includes the thread's set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. All threads of a process share the virtual address space and can access the global variables and system resources of the process.
A multitasking operating system divides the available CPU time among the threads that need it. In Windows, the Win32 API is designed for preemptive multitasking; this means that the system allocates small slices of CPU time among the competing threads. The currently executing thread is suspended when its time slice elapses, allowing another thread to run. When the system switches from one thread to another, it saves the context of the suspended thread and restores the saved context of the next thread in the queue.
Because each time slice is small (approximately 20 milliseconds), it appears that multiple threads are executing at the same time. This is actually the case on multiprocessor systems where the executable threads are distributed among the available processors. On a single processor system, however, using multiple threads does not result in more instructions being executed. In fact, the system can slow down if it is forced to keep track of too many threads.
How to Launch Win32 Applications from Visual BasicThere are two ways to launch another Win32 application from a Microsoft Visual Basic application:
It is important to understand that at creation time, the system gives each object an initial usage count of one. Then, just before CreateProcess returns, the function opens both the process and the thread object and places the process-relative handles for each in the hProcess and hThread members of the PROCESS_INFORMATION structure.
When CreateProcess opens these objects, the usage count for each increments to two. This means that before the Windows NT or Windows 2000 Executive can free the process object, the process must terminate (decrementing the usage count to one) and the parent process must call CloseHandle (decrementing the usage count to zero). To free the thread object, the thread must terminate and the parent process must close the handle to the thread object.
CAUTION: It is very important to close these handles. Failure to do so can result in a system memory leak because some Windows NT or Windows 2000 Executive objects are never destroyed.
Similar considerations are required when obtaining a process handle with OpenProcess. In this case too, the usage count is incremented by one, and unless the handle is closed, the process object will remain in memory even when the process itself has terminated.
For additional information, please see the following articles in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/129796/EN-US/ )How To Use a 32-Bit Application to Determine When a Shelled Process Ends
(http://support.microsoft.com/kb/176391/EN-US/ )How To Programmatically Close a Single Instance of a Windows-Based Program
Article ID: 129797 - Last Review: July 15, 2004 - Revision: 2.2
Contact us for more help