VB无所不能之七:VB的多线程(2)

VB无所不能之七:VB的多线程(2)
作者:钟声

友情链接(非广告,完全是出于友情):
        久违啦,我今天终于又可以写博客了,VB无所不能这个系列写作途中总是磕磕绊绊的,终于写到第七篇多线程的时候,中断了长达1个多月的时间没有更新新帖,对大家说声抱歉,由于本人这个月遇到很多棘手之事情,没有顾得上。
        好了,言归正传了,继续我们的VB无所不能之旅。
 
        上一篇我们在《VB多线程(1)》中给大家介绍了在VB中使用多线程的两个函数,一个是“ CreateThread ”另一个是“ Sleep”,这两个是使用多线程时用到的 Windowskernel32提供的API函数。
        那么,本篇将具体用一个实例说明一下这两个函数是如何使用的。
 
        我们上文提到,“VB的大部分控件都不是线程安全的”,因此,没有使用我们很熟悉的Text控件,我们为了完成本程序需要用到GDI函数绘图来实现多线程实现部分。
        我们会用到如下GDI函数,其声明及说明如下:
 
获取窗体句柄的DC:
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

设置绘制区域的背景色,也起清除作用:
Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long

输出文本:
Public 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

释放资源:
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
 
好,那么,我们开始建立程序:
 
步骤一:建立一个Module添加如下代码:
 
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的句柄!
Public formhandle As Long

OutText1() OutText1()     '过程一
         Dim i As Long
         Dim dc As Long
         Dim s As String
                dc = GetDC(formhandle) '获取窗体句柄的DC
                 For i = 1 To 100000
                        s = CStr(i)
                         Call SetBkColor(dc, &HF0F0F0)     '设置绘制区域的背景色,也起清除作用
                         Call TextOut(dc, 10, 10, s, Len(s)) '输出文本!
                         Call Sleep(40) '等待
                 Next
                 Call ReleaseDC(formhandle, dc)     '释放资源!

End Sub

OutText2() OutText2()     '和过程一类似
         Dim i As Long
         Dim dc As Long
         Dim s As String
                dc = GetDC(formhandle)
                 For i = 1 To 100000
                        s = CStr(i)
                         Call SetBkColor(dc, &HF0F0F0)
                         Call TextOut(dc, 10, 80, s, Len(s))     '文本位置改变了
                         Call Sleep(20) '延时改变了
                 Next
                 Call ReleaseDC(formhandle, dc)
End Sub
 
步骤二:建立一个窗体,添加如下组件,如图所示:
 
 
步骤三:在窗体中添加如下代码:
 
Sub Command1_Click() Sub Command1_Click()
        
         '声明了线程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()
         '保存窗体句柄全局变量,用于在form 上绘图 
        formhandle = Form1.hwnd
End Sub
 
好,我们再来看运行效果,如图所示:
 
 
好了,本文实现了一个初步的多线程调用的例子,剩下的事情就请大家八仙过海各显神通了!!
 
 
 
--------------------------------------------

 

评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值