'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
为什么没反映啊?
该怎么做?

解决方案 »

  1.   

    'FORM中
    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)
      

  2.   

    'FORM中
    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?
      

  3.   

    为什么我在VB中调试能够通过
    但是执行生成的EXE文件却一直不能执行
    报内存什么什么错误!
      

  4.   

    要把文件make成exe后内存不出错
    不能用Declare来调用api,要用类型库,把createthread等用到的api都包含在类型库中,直接调用,才能不出错。而且在多线程中不能用msgbox,只能用api  messagebox 
      

  5.   

    怎样“把createthread等用到的api都包含在类型库中”?
      

  6.   

    看下面的文章
    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创建线程
      

  7.   

    为什么我在VB里的线程不能执行?模块里:
    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)