VB调用CreateThread创建线程失败
编号:QA002166
建立日期: 1999年12月5日 最后修改日期:2000年9月21日
所属类别:
王军:
操作系统:win98
编程工具:vb
问题:我的问题是关于vb中多线程的。我用api函数createthread创建线程。具体的方法如下:
1.先添加两个标准窗体form1,form2.form3
2.form1中添加command1,command2按纽.
3.command1中代码是:
Dim lnull As Long
Dim threadidone As Long
Dim threadidtwo As Long
Dim threadhwndone As Long
Dim threadhwndtwo As Long
lnull = 0
vbbl = True
threadhwndone = CreateThread(lnull, 0, AddressOf Module1.one, _
lnull, 0, threadidone)
threadhwndtwo = CreateThread(lnull, 0, AddressOf Module1.two, _
lnull, 0, threadidtwo)
If threadhwndone = 0 Or threadhwndtwo = 0 Then MsgBox "can't _
create thread" 4.command2中代码是:
form3.show
5.再向工程中添加一标准模块。代码如下:
Public Declare Function CreateThread Lib "kernel32" _
(lpThreadAttributes As Long, ByVal dwStackSize As Long, _
lpStartAddress As Long, lpParameter As Any, ByVal _
dwCreationFlags As Long, lpThreadId As Long) As Long
Public vbbl As Boolean
Public Sub one()
Dim j As Integer
While vbbl = True
While j < 10
form1.Text1.BackColor = vbBlue
j = j + 1
Wend
j = 1
form1.Text1.BackColor = vbRed
Wend
End Sub
Public Sub two()
Dim j As Integer
form2.Show
While vbbl = True
form2.Text1 = Rnd(10) * 100
Wend
End Sub
6.使我不明白的是程序中过程one,two并没有执行,createthread返回的句柄为0。?希望能得到您的指点。我真心希望您在百忙中给我来信指点迷津。
回答:
Simply say, AddressOf doesn't behave properly in VB6. It is really lucky you didn't get a GPF when you trying to use multi-thread with CreateThread function. In other word CreateThread DOESN'T work.
You may try to use Apartment thread in vb6 that is a good or say stable way to use multi-thread in vb.
杨纲的意见:
CreateThread API 定义为:
Public Declare Function CreateThread Lib "kernel32" _
(lpThreadAttributes As Long, ByVal dwStackSize As Long, _
byval lpStartAddress As Long, lpParameter As Any, ByVal _
dwCreationFlags As Long, lpThreadId As Long) As Long
Public vbbl As Boolean
就可以了!
稽古轩的意见:
函数声明为:
Declare Function CreateThread Lib "kernel32" (ByVal lpSecurityAttributes As Long, _
ByVal dwStackSize As Long, _
ByVal lpStartAddress As Long, _
ByVal lpParameter As Long, _
ByVal dwCreationFlags As Long, _
lpThreadId As Long) As Long
调用:
hnd = CreateThread(0, 2000, AddressOf线程体名(函数名) ,参数 , 0, threadid)
即可。
此问题由James Wayne回答。
| |
|
|
| |
|
|