Question background:
Sometimes we have this need when we do programs: There is a loop that needs to run time, then the program only does other program code after waiting for the end of the loop, so the machine has been in the loop, but can not respond to other things. For CPU resources, it is a waste, then it is possible to make a loop execution, but also execute some of the other part of the code? The answer is ok, then you should use multithreaded.
related information:
Process: means that the process running on a data collection is an independent unit of the operating system for resource allocation and scheduling operation. Simply, the process is an execution of the program.
Two basic properties of the process: 1. Process is a separate unit that can have resources; 2. The process is also a basic unit that can be scheduled and assigned independently.
The purpose of the introduction process in the operating system is to enable multiple programs to perform and improve resource utilization and improve system throughput.
Thread: The line is an entity in the process and is the basic unit that is independently scheduling and assigned by the system. Threads do not have system resources, only have some essential resources in operation, but it can share all resources owned by other thread sharing processes with the same process. A plurality of threads in the same process can be implemented.
Problem implementation:
Can VB can create multi-threaded? Answer: The VB itself is not available, but can be implemented with the API function VB.
Create a thread in VB to the following API functions:
'Creating a thread API
'This API is transformed, and lpthReadAttributes are changed to the ANY type, and LPSTARTADDRESS is changed to the pass value:
'Because the function entry address is passed by a den range, if the address of the parameter is transmitted instead of the entry address of the function
'Parameters DWSTACksize is the application stack size, lpstartaddress is the function entry address
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, LpthreadId As Long) As Long
'Termination Thread API
Private Declare Function TerminateTHRead Lib "Kernel32" (Byval DwExitcode As Long) AS Long
'Activate thread API, parameter hthread is a thread handle created for CreateThread
Private Declare Function ResumeThread LIB "Kernel32" (Byval Hthread As Long) As Long
'Hang up thread API
Private Declare Function SuspendThread LIB "Kernel 32" (Byval Hthread As Long) AS Long
Learn about the above API function, please see the following instance:
Example Effect: This instance implements the background color of three picture frames.
The form layout of the example is shown in the figure:
Project window of the program:
Source code See: "VB creates multi-threaded application (2)"