linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > Basic >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·VB读取文件内容的简单方法
·用VB查询数据库记录四法
·Visual Basic串口通信程序设计
·基于VB6.0射击游戏实例
·为你的VB程序程序加密(VB新手
·VB新手常见问题解答
·什么是BASIC语言
·VB的坐标系统综述
·Visual Basic变态用法之函数指
·如何编写高质量的VB代码
·VB COM基础之ActiveX EXEs
·菜鸟入门:VB编程规约
·Visual Basic代码优化的六条定
·在VB应用程序中调用Excel2000
·VB COM基础之创建第一个COM对象
最新文章
·VB2005中开发新一代控制台应用
·VB的坐标系统综述
·变量的作用域与存活期
·VBScript 数据类型
·为你的VB程序程序加密(VB新手
·VB新手常见问题解答
·用VB开发即时战略游戏
·窗体迁移大法
·VB编程的几个API函数的应用问题
·VB编程的一些基础的问题
·利用VB6.0开发基于IIS的应用程
·在VB应用程序中调用Excel2000
·菜鸟入门:VB编程规约
·Visual Basic 安装程序的制作
·.NET和VB编程创建SQL Server
Google
 
用Visual Basic创建多线程应用程序
[ 作者:  加入时间:2007-10-19 13:13:59  来自:Linux联盟收集整理 ]
有时候我们做程序时有这样的需求:有一个需要运行时间很长的循环,那么程序只有等待循环运行结束后才执行别的程序代码,这样机器一直处于循环之中,而不能响应别的事情,对CPU资源来说是一种浪费,那么可不可以既让循环执行,又可以执行程序另外的一部分代码呢?答案是可以的,那就要用到多线程了。 37ELinux联盟
37ELinux联盟
相关知识: 37ELinux联盟
37ELinux联盟
   进程:是指程序在一个数据集合上运行的过程,是操作系统进行资源分配和调度运行的一个独立单位,简单来说进程就是程序的一次执行。 37ELinux联盟
37ELinux联盟
   进程的两个基本属性: 37ELinux联盟
37ELinux联盟
   1.进程是一个可拥有资源的独立单位; 37ELinux联盟
37ELinux联盟
   2. 进程同时又是一个可以独立调度和分配的基本单位。 37ELinux联盟
37ELinux联盟
   操作系统中引入进程的目的是为了使多个程序并发执行,以改善资源利用率及提高系统的吞吐量。 37ELinux联盟
37ELinux联盟
   线程:线是进程中的一个实体,是被系统独立调度和分配的基本单位。线程自己基本上不拥有系统资源,只拥有一些在运行中必不可少的资源,但它可与同属一个进程的其他线程共享进程所拥有的全部资源。同一个进程中的多个线程之间可以并发执行。 37ELinux联盟
37ELinux联盟
问题实现: 37ELinux联盟
37ELinux联盟
   VB可不可以创建多线程呢?答案:VB本身不可以,但用API函数VB可以实现。 37ELinux联盟
37ELinux联盟
   在VB中创建线程用到以下几个API函数: 37ELinux联盟
37ELinux联盟
'创建线程API 37ELinux联盟
37ELinux联盟
'此API经过改造,lpThreadAttributes改为Any型,lpStartAddress改为传值引用: 37ELinux联盟
37ELinux联盟
'因为函数入口地址是由形参变量传递,如果用传址那将传递形参变量的地址而不是函数的入口地址 37ELinux联盟
37ELinux联盟
' 参数dwStackSize为应用程序堆栈大小,lpStartAddress为函数入口地址 37ELinux联盟
37ELinux联盟
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, LpthreadId As Long) As Long 37ELinux联盟
37ELinux联盟
'终止线程API 37ELinux联盟
37ELinux联盟
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long 37ELinux联盟
37ELinux联盟
'激活线程API,参数hThread为CreateThread创建的线程句柄 37ELinux联盟
37ELinux联盟
Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long 37ELinux联盟
37ELinux联盟
'挂起线程API 37ELinux联盟
37ELinux联盟
Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long
37ELinux联盟
   了解完上面的API函数后请看下面的实例: 37ELinux联盟
37ELinux联盟
   实例效果:此实例实现三个图片框的背景色一起变色。 37ELinux联盟
37ELinux联盟
   实例的窗体布局见图: 37ELinux联盟
37ELinux联盟
37ELinux联盟
   程序的工程窗口: 37ELinux联盟
37ELinux联盟
37ELinux联盟
源代码如下 37ELinux联盟
37ELinux联盟
   窗体中的代码: 37ELinux联盟
37ELinux联盟
Option Explicit 37ELinux联盟
37ELinux联盟
'开始 37ELinux联盟
37ELinux联盟
Private Sub Command1_Click() 37ELinux联盟
37ELinux联盟
  On Error Resume Next 37ELinux联盟
37ELinux联盟
  With myThreadleft 37ELinux联盟
   .Initialize AddressOf Fillleft '传递过程地址给线程 37ELinux联盟
   .ThreadEnabled = True 37ELinux联盟
  End With 37ELinux联盟
37ELinux联盟
  With myThreadright 37ELinux联盟
   .Initialize AddressOf Fillright 37ELinux联盟
   .ThreadEnabled = True 37ELinux联盟
  End With 37ELinux联盟
37ELinux联盟
  With myThreadbottom 37ELinux联盟
   .Initialize AddressOf Fillbottom 37ELinux联盟
   .ThreadEnabled = True 37ELinux联盟
  End With 37ELinux联盟
37ELinux联盟
  MsgBox "多线程正在运行...,看看图片框控件的变色效果!", 64, "信息" 37ELinux联盟
37ELinux联盟
  '终止线程运行 37ELinux联盟
37ELinux联盟
  Set myThreadleft = Nothing 37ELinux联盟
  Set myThreadright = Nothing 37ELinux联盟
  Set myThreadbottom = Nothing 37ELinux联盟
37ELinux联盟
End Sub 37ELinux联盟
37ELinux联盟
'结束 37ELinux联盟
37ELinux联盟
Private Sub Command2_Click() 37ELinux联盟
  Unload Me 37ELinux联盟
End Sub 37ELinux联盟
37ELinux联盟
模块中的代码: 37ELinux联盟
37ELinux联盟
Option Explicit 37ELinux联盟
37ELinux联盟
'时间计数API 37ELinux联盟
37ELinux联盟
Private Declare Function GetTickCount Lib "kernel32" () As Long 37ELinux联盟
37ELinux联盟
'声明cls_thread类的对象变量 37ELinux联盟
Public myThreadleft As New cls_thread, myThreadright As New cls_thread, myThreadbottom As New cls_thread 37ELinux联盟
37ELinux联盟
Sub Main() 37ELinux联盟
  Load Form1 37ELinux联盟
  Form1.Show 37ELinux联盟
End Sub 37ELinux联盟
37ELinux联盟
Public Sub Fillleft() 37ELinux联盟
  Static Bkgcolor As Long 37ELinux联盟
  Dim LongTick As Long, Longcounter As Long 37ELinux联盟
  On Error Resume Next 37ELinux联盟
  For Longcounter = 0 To 3000 37ELinux联盟
   DoEvents 37ELinux联盟
   Bkgcolor = Longcounter Mod 256 37ELinux联盟
   Form1.Picture1.BackColor = RGB(Bkgcolor, 0, 0) 37ELinux联盟
   LongTick = GetTickCount 37ELinux联盟
   While GetTickCount - LongTick < 10 '延时10毫秒,下同 37ELinux联盟
   Wend 37ELinux联盟
  Next 37ELinux联盟
  Set myThreadleft = Nothing '如果循环结束则终止当前线程运行,下同 37ELinux联盟
End Sub 37ELinux联盟
37ELinux联盟
Public Sub Fillright() 37ELinux联盟
  Static Bkgcolor As Long 37ELinux联盟
  Dim LongTickValue As Long, Longcounter As Long 37ELinux联盟
  On Error Resume Next 37ELinux联盟
  For Longcounter = 0 To 3000 37ELinux联盟
   DoEvents 37ELinux联盟
   Bkgcolor = Longcounter Mod 256 37ELinux联盟
   Form1.Picture2.BackColor = RGB(0, Bkgcolor, 0) 37ELinux联盟
   LongTickValue = GetTickCount 37ELinux联盟
   While GetTickCount - LongTickValue < 10 37ELinux联盟
   Wend 37ELinux联盟
  Next 37ELinux联盟
  Set myThreadright = Nothing 37ELinux联盟
End Sub 37ELinux联盟
37ELinux联盟
Public Sub Fillbottom() 37ELinux联盟
  Static Bkgcolor As Long 37ELinux联盟
  Dim LongTick As Long, Longcounter As Long 37ELinux联盟
  On Error Resume Next 37ELinux联盟
  For Longcounter = 0 To 3000 37ELinux联盟
   DoEvents 37ELinux联盟
   Bkgcolor = Longcounter Mod 256 37ELinux联盟
   Form1.Picture3.BackColor = RGB(0, 0, Bkgcolor) 37ELinux联盟
   LongTick = GetTickCount 37ELinux联盟
   While GetTickCount - LongTick < 10 37ELinux联盟
   Wend 37ELinux联盟
  Next 37ELinux联盟
  Set myThreadright = Nothing 37ELinux联盟
End Sub 37ELinux联盟
37ELinux联盟
类模块中的代码: 37ELinux联盟
37ELinux联盟
'功能:创建多线程类,用于初始化线程。 类名:cls_Thread 37ELinux联盟
37ELinux联盟
'参数:LongPointFunction 用于接收主调过程传递过来的函数地址值 37ELinux联盟
37ELinux联盟
'调用方法:1.声明线程类对象变量 Dim mythread as cls_Thread 37ELinux联盟
37ELinux联盟
' 2.调用形式:With mythread 37ELinux联盟
37ELinux联盟
' .Initialize AddressOf 自定义过程或函数名 '(初始化线程) . 37ELinux联盟
37ELinux联盟
' .ThreadEnabled = True '(设置线程是否激活) 37ELinux联盟
37ELinux联盟
' End With 37ELinux联盟
37ELinux联盟
' 3.终止调用: Set mythread = Nothing 37ELinux联盟
37ELinux联盟
' Crate By : 陈宇 On 2004.5.10 Copyright(C).Ldt By CY-soft 2001--2004 37ELinux联盟
37ELinux联盟
' Email:4y4ycoco@163.com            37ELinux联盟
37ELinux联盟
' Test On: VB6.0+Win98 AND VB6.0+WinXP It's Pass ! 37ELinux联盟
37ELinux联盟
Option Explicit 37ELinux联盟
37ELinux联盟
'创建线程API 37ELinux联盟
37ELinux联盟
'此API经过改造,lpThreadAttributes改为Any型,lpStartAddress改为传值引用: 37ELinux联盟
37ELinux联盟
'因为函数的入口地址由形参变量传递,如果用传址那将传递形参变量的地址而不是函数的入口地址 37ELinux联盟
37ELinux联盟
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, LpthreadId As Long) As Long 37ELinux联盟
37ELinux联盟
'终止线程API 37ELinux联盟
37ELinux联盟
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long 37ELinux联盟
37ELinux联盟
'激活线程API 37ELinux联盟
37ELinux联盟
Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long 37ELinux联盟
37ELinux联盟
'挂起线程API 37ELinux联盟
37ELinux联盟
Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long 37ELinux联盟
Private Const CREATE_SUSPENDED = &H4 '线程挂起常量 37ELinux联盟
37ELinux联盟
'自定义线程结构类型 37ELinux联盟
37ELinux联盟
Private Type udtThread 37ELinux联盟
  Handle As Long 37ELinux联盟
  Enabled As Boolean 37ELinux联盟
End Type 37ELinux联盟
37ELinux联盟
Private meTheard As udtThread 37ELinux联盟
37ELinux联盟
'初始化线程 37ELinux联盟
Public Sub Initialize(ByVal LongPointFunction As Long) 37ELinux联盟
37ELinux联盟
  Dim LongStackSize As Long, LongCreationFlags As Long, LpthreadId As Long, LongNull As Long 37ELinux联盟
  On Error Resume Next 37ELinux联盟
  LongNull = 0 37ELinux联盟
  LongStackSize = 0 37ELinux联盟
  LongCreationFlags = CREATE_SUSPENDED '创建线程后先挂起,由程序激活线程 37ELinux联盟
  '创建线程并返线程句柄 37ELinux联盟
  meTheard.Handle = CreateThread(LongNull, LongStackSize, ByVal LongPointFunction, LongNull, LongCreationFlags, LpthreadId) 37ELinux联盟
37ELinux联盟
  If meTheard.Handle = LongNull Then 37ELinux联盟
   MsgBox "线程创建失败!", 48, "错误" 37ELinux联盟
  End If 37ELinux联盟
End Sub 37ELinux联盟
37ELinux联盟
'获取线程是否激活属性 37ELinux联盟
37ELinux联盟
Public Property Get ThreadEnabled() As Boolean 37ELinux联盟
  On Error Resume Next 37ELinux联盟
  Enabled = meTheard.Enabled 37ELinux联盟
End Property 37ELinux联盟
37ELinux联盟
'设置线程是否激活属性 37ELinux联盟
37ELinux联盟
Public Property Let ThreadEnabled(ByVal Newvalue As Boolean) 37ELinux联盟
  On Error Resume Next 37ELinux联盟
  '若激活线程(Newvalue为真)设为TRUE且此线程原来没有激活时激活此线程 37ELinux联盟
37ELinux联盟
  If Newvalue And (Not meTheard.Enabled)  Then 37ELinux联盟
   ResumeThread meTheard.Handle 37ELinux联盟
   meTheard.Enabled = True 37ELinux联盟
  Else '若激活线程(Newvalue为真)且此线程原来已激活则挂起此线程 37ELinux联盟
   If meTheard.Enabled Then 37ELinux联盟
    SuspendThread meTheard.Handle 37ELinux联盟
    meTheard.Enabled = False 37ELinux联盟
   End If 37ELinux联盟
  End If 37ELinux联盟
End Property 37ELinux联盟
37ELinux联盟
'终止线程事件 37ELinux联盟
37ELinux联盟
Private Sub Class_Terminate() 37ELinux联盟
  On Error Resume Next 37ELinux联盟
  Call TerminateThread(meTheard.Handle, 0) 37ELinux联盟
End Sub 37ELinux联盟
37ELinux联盟
   总结: 37ELinux联盟
37ELinux联盟
   本程序的缺点是程序运行时CPU占用率高。 37ELinux联盟
37ELinux联盟
   至此全部源代码结束,在实例的基础上可以根据个人需要做出不同的多线程应用程序,可以用此类模块创建ActiveX DLL,然后引用这个DLL来进行调用。
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·Visual Basic 安装程序的制作  (2007-10-29 14:37:01)
 ·Visual Basic串口通信程序设计  (2007-10-29 14:33:03)
 ·Visual Basic6.0网络编程的五大散手  (2007-10-19 13:16:05)
 ·Visual Basic变态用法之函数指针  (2007-10-19 13:12:05)
 ·Visual Basic COM基础之兼容性  (2007-10-19 13:07:40)
 ·Visual Basic COM基础之事件  (2007-10-19 13:01:19)
 ·Visual Basic COM基础之更多属性  (2007-10-19 12:59:38)
 ·Visual Basic COM基础之属性  (2007-10-19 12:58:54)
 ·Visual Basic COM基础之类的建立  (2007-10-19 12:58:10)
 ·Visual Basic COM基础讲座  (2007-10-19 12:57:22)