7. omnipotent VB: multithreading of VB (2)

Source: Internet
Author: User

7. omnipotent VB: multithreading of VB (2)Author: bell

Links (non-advertising, completely out of friendship ):Cloud detection and removal of Kingsoft drug overlord! Free! 2011 download link!

For a long time, I can finally write a blog today. VB is omnipotent. This series of writing is always bumpy. When I finally write the seventh multi-thread article, I was interrupted for more than a month and did not update new posts. I am sorry, because I have encountered many difficult things this month, so I have no worries.
Now, let's get down to the truth and continue our VB omnipotent journey. In the previous article, we introduced two functions that use multithreading in VB in "VB multithreading (1)". One is" Createthread"The other is" SleepThese two are used when multithreading is used. WindowsOf Kernel32Provided API functions. In this article, we will use an example to illustrate how these two functions are used. As we mentioned above, "most of the controls of VB are NOT thread-safe." Therefore, text controls that we are very familiar with are not used, in order to complete this program, we need to use the GDI function plot to implement multi-threaded implementation. We will use the following GDI function. Its declaration and description are as follows: Obtain the dc of the form handle:

Public declare Function GetdcLib "USER32" (byval hwnd as long) as long

You can also clear the background color of the drawn area:Public declare Function SetbkcolorLib "GDI32" (byval HDC as long, byval crcolor as long) as long

Output text:
Public declare FunctionTextoutLib "GDI32" alias "textouta" (byval HDC as long, byval X as long, byval y as long, byval lpstring as string, byval ncount as long) as long

Release resources:
Public declare FunctionReleasedcLib "USER32" (byval hwnd as long, byval HDC as long) as long, so let's start to build the program:Step 1: Create a module and add the following code:Declare function getdc lib "USER32" () Declare function getdc lib "USER32" (byval hwnd as long) as long
Declare function setbkcolor lib "GDI32" () Declare function setbkcolor lib "GDI32" (byval HDC as long, byval crcolor as long) as long
Declare function textout lib "GDI32" alias "textouta" () Declare function textout lib "GDI32" alias "textouta" (byval HDC as long, byval X as long, byval y as long, byval lpstring as string, byval ncount as long) as long
Declare function releasedc lib "USER32" () Declare function releasedc lib "USER32" (byval hwnd as long, byval HDC as long) as long

Declare function createthread lib "Kernel32" () 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

Declare sub sleep lib "Kernel32" () Declare sub sleep lib "Kernel32" (byval dwmilliseconds as long)

'Form handle globally!
Public formhandle as long

Outtext1 () outtext1 () 'process 1
Dim I as long
Dim DC as long
Dim s as string
Dc = getdc (formhandle) 'obtain the dc of the form handle
For 1 to 100000
S = CSTR (I)
Call setbkcolor (DC, & hf0f0f0) 'is used to set the background color of the drawn area.
Call textout (DC, 10, 10, S, Len (s) 'to output the text!
Call sleep (40) 'wait
Next
Call releasedc (formhandle, DC) 'to release resources!

End sub

Outtext2 () outtext2 () 'is similar to process 1
Dim I as long
Dim DC as long
Dim s as string
Dc = getdc (formhandle)
For 1 to 100000
S = CSTR (I)
Call setbkcolor (DC, & hf0f0f0)
Call textout (DC, 10, 80, S, Len (s) 'Text Location changed
Call sleep (20) 'latency changed
Next
Call releasedc (formhandle, DC)
End subStep 2: create a form and add the following components ,:Step 3: Add the following code to the form:Sub commandementclick () sub commandementclick ()

'Declares the thread ID
Dim threadid1 as long
Dim threadid2 as long

Call createthread (null, byval O &, addressof module1.outtext1, varptr (0), byval 0 &, threadid1)
Call createthread (null, byval 0 &, addressof module1.outtext2, varptr (0), byval 0 &, threadid2)

End sub

Sub form_load () sub form_load ()
'Save the global variables of the form handle for drawing on the form.
Formhandle = form1.hwnd
End sub is good. Let's take a look at the running effect. Well, this article provides a preliminary example of multi-threaded calling. The rest of this article will show you all !!

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.