网站首页

家园论坛

老版论坛

家园博客

业界新闻

技术文档

下载中心

速查中心

图片中心

硬件资讯
上一篇:VB创建多线程应用程序(一) 下一篇:用VB6.0编写自我升级的程序(一)
VB创建多线程应用程序(二)

来源: 作者: 添加日期:2005-9-4 19:19:56 点击次数:

窗体中的代码:

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)

           LongTickValue = GetTickCount

           While GetTickCount - LongTickValue < 10

           Wend

       Next

Set myThreadright = Nothing

End Sub

 

Public Sub Fillbottom()

       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.Picture3.BackColor = RGB(0, 0, Bkgcolor)

           LongTick = GetTickCount

           While GetTickCount - LongTick < 10

           Wend

       Next

       Set myThreadright = Nothing

End Sub

类模块中的代码:

 

'功能:创建多线程类,用于初始化线程。   类名:cls_Thread 

'参数:LongPointFunction 用于接收主调过程传递过来的函数地址值

'调用方法:1.声明线程类对象变量 Dim mythread as cls_Thread

'          2.调用形式:With mythread

'                         .Initialize AddressOf 自定义过程或函数名 '(初始化线程) .

'         

 
设为首页 | 加入收藏 | 业务办理 | 友情链接 | 论坛版面 | 浙ICP备07502118号 |