7. omnipotent VB: multithreading of VB (1)

Source: Internet
Author: User
7. omnipotent VB: multithreading of VB (1) Author: bell  Digress:Recently, I have written several series of "VB omnipotent". The original intention of this series of articles is to subvert the prejudice of many programmers on the "non-Object-Oriented" language "VB! Of course, I have expected that this statement will inevitably lead to the "Siege" of people who do not know the truth. However, I still want to continue to write this statement, which is better than words, here I also want to say something to a large number of netizens. If you want to question some of my ideas in this article, you are welcome to come up with the true evidence and debate with me. "Wu Mao dang"Language Hengjia kill. Declare again about "omnipotent ":The series in this article is "VB omnipotent". It is not as omnipotent as Sun Wukong's 72 changes. If you want to change a mosquito to a mosquito, you want to change an elephant to an elephant, it is impossible to use VB to solve the problem of "VB is omnipotent". No language can be "omnipotent ", the "omnipotent" here is an "adverbial", not an "attribute". OK? Okay, let's continue. Recently, some netizens strongly asked me to write an article about VB multithreading. In my blog message, I shouted "A headache "!! Yes, it is a headache for VB to process multiple threads. It is a headache for VB to process images, hook programs, and pointers. There are only the following headaches: 1. VB does not have a multi-thread processing mechanism. 2. Most of the controls of VB are NOT thread-safe. 3. The program crashes due to serious errors, but it is still designed to meet the requirements of netizens, I am still writing an article with a hard head. If something is wrong, please laugh at it !! I. First, let's get to know the thread functions provided by Windows Kernel32.  Createthread   Note:Microsoft provides the createthread function for creating a new thread in Windows API. Overview:
When CreateProcess is called, The system creates a process and a main thread. Createthread creates a new thread on the basis of the main thread. The steps are as follows:
1. Allocate a thread ID/handle to the kernel object for management. It is returned by createthread.
2. Set the thread exit code to still_active, and set the thread suspension count to 1.
3. Allocate the Context Structure
4. Two pages of physical storage are allocated to prepare the stack. The protection page is set to page_readwrite, and page 2nd is set to page_guard.
5. The lpstartaddr and lpvthread values are placed on the top of the stack to make them the parameters passed to startofthread.
6. Point the stack pointer of the context structure to the top of the stack (step 1) command pointer to the startofthread function. VB Function declaration:Declare function createthread lib "Kernel32" (_ lpthreadattributes as any, _ byval dwstacksize as long, _ byval lpstartaddress as long, _ byval lpparameter as long, _ byval dwcreationflags as long, _ lpthreadid as long) as long Parameter description:
Lpthreadattributes:
Pointer to the security_attributes type structure. Ignore this parameter in Windows 98. In Windows NT, it is set to null, indicating that the default value is used.
Dwstacksize:Thread stack size, usually = 0. In any case, Windows dynamically prolongs the stack size as needed.
Lpstartaddress:Pointer to a thread function. The format is @ function name. The function name is not restricted, but must be declared in the following column: DWORD winapi threadproc (lpvoid pparam). If the format is incorrect, the call will fail.
Lpparameter:A parameter passed to a thread function is a pointer to a structure. It is null if no parameter is required.
Dwcreationflags:Thread flag. Optional values:
Create_suincluded: Creates a suspended thread.
0: activated immediately after creation.
Lpthreadid:Save the ID of the new thread. Return Value:
If the function succeeds, the thread handle is returned. If the function fails, false is returned.
Function Description:
Create a thread. Sleep   VB statement:
Private declare sub sleep lib "Kernel32" (byval dwmilliseconds as long)

Call:
Sleep 3000 'execution with a delay of 3 seconds 'This is sleep, which is used to make the drawing frequency of the two threads inconsistent, and the effect is obvious.
Public declare sub sleep lib "Kernel32" (byval dwmilliseconds as long) Well, today we know these two functions, write them here today, and tomorrow we will provide routines, let's see how VB uses these functions to complete multi-threaded tasks ..

''' A book written by Bell on Java:  Java programmers:
Preface, Directory
Zhuo Yue network sales Link
China-Pub sales Link
Dangdang sales Link Commemorative post on "Java programmer, work day"

Download an electronic version of "Java programmer, work day"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.