分享
 
 
 

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

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

源代码如下:

窗体中的代码:

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 自定义过程或函数名 '(初始化线程) .

' .ThreadEnabled = True '(设置线程是否激活)

' End With

' 3.终止调用: Set mythread = Nothing

' Crate By : 陈宇 On 2004.5.10 Copyright(C).Ldt By CY-soft 2001--2004

' Email:4y4ycoco@163.com

' Test On: VB6.0+Win98 AND VB6.0+WinXP It's Pass !

Option Explicit

'创建线程API

'此API经过改造,lpThreadAttributes改为Any型,lpStartAddress改为传值引用:

'因为函数的入口地址由形参变量传递,如果用传址那将传递形参变量的地址而不是函数的入口地址

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

'终止线程API

Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long

'激活线程API

Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long

'挂起线程API

Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long

Private Const CREATE_SUSPENDED = &H4 '线程挂起常量

'自定义线程结构类型

Private Type udtThread

Handle As Long

Enabled As Boolean

End Type

Private meTheard As udtThread

'初始化线程

Public Sub Initialize(ByVal LongPointFunction As Long)

Dim LongStackSize As Long, LongCreationFlags As Long, LpthreadId As Long, LongNull As Long

On Error Resume Next

LongNull = 0

LongStackSize = 0

LongCreationFlags = CREATE_SUSPENDED '创建线程后先挂起,由程序激活线程

'创建线程并返线程句柄

meTheard.Handle = CreateThread(LongNull, LongStackSize, ByVal LongPointFunction, LongNull, LongCreationFlags, LpthreadId)

If meTheard.Handle = LongNull Then

MsgBox "线程创建失败!", 48, "错误"

End If

End Sub

'获取线程是否激活属性

Public Property Get ThreadEnabled() As Boolean

On Error Resume Next

Enabled = meTheard.Enabled

End Property

'设置线程是否激活属性

Public Property Let ThreadEnabled(ByVal Newvalue As Boolean)

On Error Resume Next

'若激活线程(Newvalue为真)设为TRUE且此线程原来没有激活时激活此线程

If Newvalue And (Not meTheard.Enabled) Then

ResumeThread meTheard.Handle

meTheard.Enabled = True

Else '若激活线程(Newvalue为真)且此线程原来已激活则挂起此线程

If meTheard.Enabled Then

SuspendThread meTheard.Handle

meTheard.Enabled = False

End If

End If

End Property

'终止线程事件

Private Sub Class_Terminate()

On Error Resume Next

Call TerminateThread(meTheard.Handle, 0)

End Sub

总结:

本程序的缺点是程序运行时CPU占用率高。

至此全部源代码结束,在实例的基础上可以根据个人需要做出不同的多线程应用程序,可以用此类模块创建ActiveX DLL,然后引用这个DLL来进行调用。希望对大家有用,若大家找到一种更好的实现方法且CPU占用率低的程序请通知我,程序中有什么Bug请Email,谢谢。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有