昨晚2:30的时候还没睡着,觉得有必要把vb编写多线程程序再次写一次;主要是以前忽略的细节和重要的环节;今天在公司打开一年多没用的vb,写了如下的代码;想写多线程的朋友可以调试一下看看,关于多线程的任务模式,同步和互斥,临界资源和临界区(文中提到)欢迎跟帖讨论;
'请将该部分数据保存为 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为未公开函数)!!