'FORM中
Private Sub Command1_Click()
hThread1 = CreateThread(0&, 0&, AddressOf ThreadProc1, _
0&, 0&, 1)
hThread2 = CreateThread(0&, 0&, AddressOf ThreadProc2, _
0&, 0&, 2)
End SubPrivate Sub Command2_Click()
If hThread1 <> 0 Then TerminateThread hThread1, 0
If hThread2 <> 0 Then TerminateThread hThread2, 0
End Sub
'模块中
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPublic hThread1 As Long, hThread2 As LongPublic Sub ThreadProc1(ByRef Param As Long)
While True
'Sleep 2000
MsgBox "TH1"
Wend
End SubPublic Sub ThreadProc2(ByRef Param As Long)
While True
'Sleep 2000
MsgBox "TH2"
Wend
End Sub
为什么没反映啊?
该怎么做?
Private Sub Command1_Click()
hThread1 = CreateThread(0&, 0&, AddressOf ThreadProc1, _
0&, 0&, 1)
hThread2 = CreateThread(0&, 0&, AddressOf ThreadProc2, _
0&, 0&, 2)
End SubPrivate Sub Command2_Click()
If hThread1 <> 0 Then TerminateThread hThread1, 0
If hThread2 <> 0 Then TerminateThread hThread2, 0
End Sub
'模块中
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPublic hThread1 As Long, hThread2 As LongPublic Sub ThreadProc1(ByRef Param As Long)
While True
'Sleep 2000
MsgBox "TH1"
Wend
End SubPublic Sub ThreadProc2(ByRef Param As Long)
While True
'Sleep 2000
MsgBox "TH2"
Wend
End Sub
为什么没反映啊?
该怎么做?
解决方案 »
- 关于Datareport打印自定义纸张的奇怪问题
- 帮忙来看一下!
- 用WINSOCK发送邮件的问题?
- 一个有难度的实际问题:想把用户的笔迹图片和公章的图片开发成控件,怎么做啊?
- 怎样将access查询的数据导出excel (包括字段名一起导出)
- 想问一个关于recordset的问题
- 用ado对象通过oledb连接数据库时参数如何设置阿?
- 用了AutoFilter,Select后,怎样对结果集进行遍历,并选择其中的最大值的行号?
- 求救
- 这个题目只有5分,因为其够简单
- 不用datagrid帮定data怎么把access中的内容显示出来!在线等!
- DBGrid 控件中列的数据字段能否在运行中指定(不在设计时指定,但datasource在设计时绑定到data1)?
Private Sub Command1_Click()
hThread1 = CreateThread(0&, 0&, AddressOf ThreadProc1, _
0&, 0&, 1)
hThread2 = CreateThread(0&, 0&, AddressOf ThreadProc2, _
0&, 0&, 2)
End Sub最后不能用数字1或2,只能是
dim ThreadID as long
hThread1 = CreateThread(0&, 0&, AddressOf ThreadProc1, _
0&, 0&, ThreadID)
Private Sub Command1_Click()
Dim ThreadID As Long
hThread1 = CreateThread(0&, 0&, AddressOf ThreadProc1, _
0&, 0&, ThreadID)
hThread2 = CreateThread(0&, 0&, AddressOf ThreadProc2, _
0&, 0&, ThreadID)
End Sub这样对吗?都用同样的ThreadID?
但是执行生成的EXE文件却一直不能执行
报内存什么什么错误!
不能用Declare来调用api,要用类型库,把createthread等用到的api都包含在类型库中,直接调用,才能不出错。而且在多线程中不能用msgbox,只能用api messagebox
http://www.csdn.net/develop/Read_Article.asp?Id=16529
通过使用类型库提高VB调用DLL函数的性能也看http://vbaccelerator.com/home/VB/index.asp中Type Libraries 中的API Calls used for Threading (threadapi.tlb)在www.pscode.com的vb项目中搜索thread,也有许多的例子。里面讲了为什么不能直接在vb6中使用createthread创建线程
Public Sub ThreadProc1()
MsgBox "send"
Dim dc As Long
dc = GetDC(formhandle)
While True
If IsInit Then
Call Console.Send(GetServer, GetUID, GetPWD, GetDataBase)
Else
Unload Console
End If
Call Sleep(GetTimes)
Wend
Call ReleaseDC(formhandle, dc)
Call EnterCriticalSection(sect)
Call LeaveCriticalSection(sect)
End SubPublic Sub ThreadProc2()
Dim dc As Long
dc = GetDC(formhandle)
While True
If IsInit Then
Call Console.Recieve(GetServer, GetUID, GetPWD, GetDataBase)
Else
Unload Console
End If
Call Sleep(500)
Wend
Call ReleaseDC(formhandle, dc)
Call EnterCriticalSection(sect)
Call LeaveCriticalSection(sect)
End Sub
form里:
Dim threadid1 As Long
Dim threadid2 As Long
CreateThread Null, ByVal O&, AddressOf ServiceModule.ThreadProc1, VarPtr(0), ByVal 0&, threadid1
Call CreateThread(Null, ByVal 0&, AddressOf ServiceModule.ThreadProc2, VarPtr(0), ByVal 0&, threadid2)