VB创建多线程应用程序(二)

文章作者 100test 发表时间 2007:05:12 12:20:40
来源 100Test.Com百考试题网


计算机等级考试训练软件《百宝箱》

源代码如下:
窗体中的代码:
Option Explicit

’开始
Private Sub Command1_Click()

On Error Resume Next

With myThreadleft
.Initialize AddressOf Fillleft ’传递过程地址给线程
.ThreadEnabled = True
End With
With myThreadright
.Initialize AddressOf Fillright
.ThreadEnabled = True
End With
With myThreadbottom
.Initialize AddressOf Fillbottom
.ThreadEnabled = True
End With

MsgBox "多线程正在运行...,看看图片框控件的变色效果!", 64, "信息"

’终止线程运行
Set myThreadleft = Nothing
Set myThreadright = Nothing
Set myThreadbottom = Nothing

End Sub

’结束
Private Sub Command2_Click()
Unload Me
End Sub
模块中的代码:
Option Explicit

’时间计数API
Private Declare Function GetTickCount Lib "kernel32" () As Long

’声明cls_thread类的对象变量
Public myThreadleft As New cls_thread, myThreadright As New cls_thread, myThreadbottom As New cls_thread

Sub Main()
Load Form1
Form1.Show
End Sub

Public Sub Fillleft()
Static Bkgcolor As Long
Dim LongTick As Long, Longcounter As Long
On Error Resume Next
For Longcounter = 0 To 3000
DoEvents
Bkgcolor = Longcounter Mod 256
Form1.Picture1.BackColor = RGB(Bkgcolor, 0, 0)
LongTick = GetTickCount
While GetTickCount - LongTick < 10 ’延时10毫秒,下同
Wend
Next
Set myThreadleft = Nothing ’如果循环结束则终止当前线程运行,下同
End Sub

Public Sub Fillright()
Static Bkgcolor As Long
Dim LongTickValue As Long, Longcounter As Long
On Error Resume Next
For Longcounter = 0 To 3000
DoEvents
Bkgcolor = Longcounter Mod 256
Form1.Picture2.BackColor = RGB(0, Bkgcolor, 0)

相关文章


如何编写异常安全的C 代码
C 箴言:理解隐式接口和编译期多态
编程指南:C语言编程易犯毛病集合
VB创建多线程应用程序(一)
VB创建多线程应用程序(二)
在WPS文档中更改图片原来可以更简单省事
Excel2007中选择一个或多个工作表
在Excel工作表区域内快速移动或滚动
在WPS2005文字中插入组织结构图的技巧
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛