vb实现多线程!
2008-04-10 03:07:35来源:互联网 阅读 ()
''''请将该部分数据保存为 FORM1.frm 文件
VERSION 5.00
Begin VB.Form Form1
Caption = "多线程"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 6450
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 6450
StartUpPosition = 3 ''''窗口缺省
Begin VB.TextBox Text1
Height = 270
Left = 960
TabIndex = 2
Text = "2"
Top = 2760
Width = 2415
End
Begin VB.CommandButton Command2
Caption = "返回"
Height = 255
Left = 3480
TabIndex = 1
Top = 2760
Width = 1455
End
Begin VB.CommandButton Command1
Caption = "Start Count"
Height = 255
Left = 3480
TabIndex = 0
Top = 240
Width = 1455
End
Begin VB.Label Label1
AutoSize = -1 ''''True
Caption = "主线程执行结果测试:"
Height = 180
Left = 600
TabIndex = 3
Top = 2400
Width = 1710
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
''''下载地址:http://www.bssoft.com.cn/vbThread.rar
Private Sub Command1_Click()
''''声明了线程ID
Dim threadid1 As Long
Dim threadid2 As Long
''''参数一,lpThreadAttributes 线程安全属性,传递为NULL
''''参数二,dwStackSize ,线程堆栈大小,可以为0,表示堆栈和此应用堆栈相同
''''参数三,lpstartAddress ,执行函数地址,用AddressOf 获取
''''参数四,lpParameter ,执行函数的参数地址,可以是一个记录或者是别的类型,用VarPtr获取参数地址(varptr为未公开函数)!!
''''参数五,dwCreationFlags ,表示线程创建后的状态!,0表示立即运行,create_SUSPENDED表示线程挂起
''''参数六,lpThreadID 表示分配给线程的线程号
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
Private Sub Command2_Click()
''''该事件运行于主线程!
Dim i As Long
i = CLng(Text1.Text)
Text1.Text = CStr(i * i) ''''不要点击次数太多,LONG 类型会溢出
End Sub
Private Sub Form_Load()
''''保存窗体句柄全局变量,用于在form 上绘图
formhandle = Form1.hwnd
End Sub
----------------------------------
''''请将该部分数据保存为 Module1.bas 文件
Attribute VB_Name = "Module1"
''''线程安全属性数据结构;
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
''''这个是用于多线程访问临界资源同步Api的数据结构
Public Type CRITICAL_SECTION
dummy As Long
End Type
''''为什么用GDI 函数绘图?原因等下再讲
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
''''请注意;createThread APi声明已被我修改过,修改的地方请自行参照APIView复制的内容
Public 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
''''这个是sleep,作用就是让两个线程绘图频率不一致,效果才明显。
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Sub EnterCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION) ''''进入临界区
Public Declare Sub LeaveCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash