form1里有两个command,两个text。command1里的代码是这样的:
Private Sub Command1_Click()
Dim i As Single
For i = 1 To 30000
    Text1.Refresh
    Text1.Text = i
Next i
End Sub
command2里的代码是这样的:
Private Sub Command2_Click()
Dim j As Single
For j = 1 To 30000
    Text2.Refresh
    Text2.Text = j
Next j
End Sub
我的目的就是点击command1后,再点击command2,使它们能同时运行。我这样是想锻炼一下多线程的用法,请高手指导一下这样的程序应该怎么写才能实现上述功能。

解决方案 »

  1.   

    呵呵,这样是实现不了多线程的!你点command1后,如正在运行,点command2是没有反应的,因为它们在同一线程!要用多线程,看看activeX exe
      

  2.   

    多线程好像要用CreateThread()吧,不过VB用极其不稳定,我就深受其害。
    创建多线程,首先代码要放在模块里面(注意备份,可能正在运行VB就死掉了)
    Public Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long '修改过的申明然后找个地方
    CreateThread(Nul,ByVal 0&,AddressOf ***你的函数/过程名称***,ByVal 0,0,**你用来保存线程号的变量***)
      

  3.   

    修改你的代码,做个假多线程,实现你的要求。(注:此段代码只能由command1先启动,如果要实现不论按钮次序,代码就留给你完成了,不准完全依懒别人。)
    ============
    Dim blCMD1, blCMD2 As BooleanPrivate Sub Command1_Click()
        blCMD1 = True
        abcdef
        blCMD1 = False
    End SubPrivate Sub Command2_Click()
        blCMD2 = True
    End SubSub abcdef()
        Dim i As Single
        Dim j As Single
        
        Do
            If i = 30000 And blCMD2 Then GoTo asdfasdf
            Text1.Text = i
            DoEvents
            i = i + 1        
            If blCMD2 = False Then GoTo qwertyu
            
    asdfasdf:
            Text2.Text = j
            j = j + 1
            If j = 30000 Then Exit Do
    qwertyu:
        Loop
        blCMD2 = False
    End Sub
      

  4.   

    Dim blCMD1, blCMD2 As Boolean
    Private Sub Command1_Click()
        blCMD1 = True
        abcdef
    End Sub
    Private Sub Command2_Click()
        blCMD2 = True
        abcdef
    End Sub
    Sub abcdef()
        Dim i, j As Single
        Do
            If Not blCMD1 Then GoTo asdfasdf
            Text1.Text = i
            DoEvents
            i = i + 1
            If i = 30000 Then blCMD1 = False
            If blCMD2 = False Then GoTo qwertyu
    asdfasdf:
            Text2.Text = j
            DoEvents
            j = j + 1
            If j = 30000 Then blCMD2 = False
    qwertyu:
            If Not blCMD1 And Not blCMD2 Then Exit Do
        Loop
    End Sub
      

  5.   

    出丑了,试运行还是有bug,不好意思,再次修改代码。(错误还是论坛软件造成的,竟然没发现让回帖人修改编辑的功能)
    Dim blCMD1, blCMD2, BOOLabcdef As Boolean
    Private Sub Command1_Click()
        blCMD1 = True
        If Not BOOLabcdef Then abcdef
    End Sub
    Private Sub Command2_Click()
        blCMD2 = True
        If Not BOOLabcdef Then abcdef
    End Sub
    Sub abcdef()
        Dim i, j As Single
        BOOLabcdef = True
        Do
            If Not blCMD1 Then GoTo asdfasdf
            Text1.Text = i
            DoEvents
            i = i + 1
            If i = 30000 Then blCMD1 = False
            If blCMD2 = False Then GoTo qwertyu
    asdfasdf:
            Text2.Text = j
            DoEvents
            j = j + 1
            If j = 30000 Then blCMD2 = False
    qwertyu:
            If Not blCMD1 And Not blCMD2 Then Exit Do
        Loop
        BOOLabcdef = False
    End Sub
      

  6.   

    http://community.csdn.net/Expert/topic/5683/5683251.xml?temp=.7626001