您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 招标投标 > VB-多线程实例代码
VB多线程实例代码最近因为想写一个多线程的工具,以前也没写过;所以查了些VB多线程资料;方法虽然有很多,但我觉得这种是最简单而且又安全的一种真真的VB多线程方法。大家往下看吧,分2大步骤;1、新建一个工程,类型选择ActiveXExe,工程重命名为TestExe在工程中添加一个Form,放上一个Timer控件。注:Timer控件interval值别忘了设置。将Class1改名为clsTest,注意其Instancing要设置为5-MultiUse。以下是clsTest类模块其代码:OptionExplicitPrivateDeclareSubSleepLibkernel32(ByValdwMillisecondsAsLong)PrivateWithEventsoTimerAsTimerPrivatefrmTmpAsForm1PrivatelTotalLoopAsLongPrivatebDoStopAsBooleanPrivatelIntervalAsLongPublicEventProgress(ByVallProgressAsLong)PublicSubStartSub(ByVallTotalAsLong)lTotalLoop=lTotaloTimer.Enabled=TrueEndSubPublicSubStopSub()bDoStop=TrueEndSubPrivateSubClass_Initialize()SetfrmTmp=NewForm1LoadfrmTmpSetoTimer=frmTmp.Timer1oTimer.Enabled=FalsebDoStop=FalselInterval=1EndSubPrivateSubDoLoop()DimiAsLongFori=0TolTotalLoopSleep(lInterval)RaiseEventProgress(i)IfbDoStop=TrueThenExitSubEndIfNextEndSubPrivateSubClass_Terminate()UnloadfrmTmpSetfrmTmp=NothingSetoTimer=NothingEndSubPrivateSuboTimer_Timer()oTimer.Enabled=FalseDoLoopEndSubPublicPropertyGetlMillisecs()AsLonglMillisecs=lIntervalEndPropertyPublicPropertyLetlMillisecs(ByValvNewValueAsLong)lInterval=vNewValueEndProperty在TestExe工程属性的通用页下,选择线程缓冲沲,线程数值设值为2。则调用此ActiveXExe最多能同时有两个线程,更多的请求将放置于队列中。编译生成TestExe2、下面来测试我们的多线程程序:新建一个标准Exe工程,在“引用”中选择刚刚编译生成的TestExe.exe在Form1中添加两个ListBox,两个CommandButton,Command1为开始,Command2为停止以下是Project1.Form1的代码:OptionExplicitPrivateWithEventsoTest1AsTestEXE.clsTestPrivateWithEventsoTest2AsTestEXE.clsTestPrivateSubCommand1_Click()SetoTest1=NewTestEXE.clsTestoTest1.lMillisecs=100oTest1.StartSub(1000)SetoTest2=NewTestEXE.clsTestoTest2.lMillisecs=100oTest2.StartSub(1000)EndSubPrivateSubCommand2_Click()oTest1.StopSuboTest2.StopSubEndSubPrivateSubForm_Unload(CancelAsInteger)SetoTest1=NothingSetoTest2=NothingEndSubPrivateSuboTest1_Progress(ByVallProgressAsLong)List1.AddItemlProgressList1.ListIndex=List1.ListCount-1EndSubPrivateSuboTest2_Progress(ByVallProgressAsLong)List2.AddItemlProgressList2.ListIndex=List2.ListCount-1EndSub大功告成,启动运行form1,点击Command1,怎么样,看见来效果了吗?
本文标题:VB-多线程实例代码
链接地址:https://www.777doc.com/doc-4990941 .html