VB程序的移交控制权的问题:
 
有两个Command按钮,点击后产生Click事件。点击Command1,有一个无止境的数据计算循环,一直处于运算状态。 点击Command2,就从把command1的运算状态停止掉。     我的COMMAND1的程序:
Private Sub Command1_Click() 
i = 0 
BZ1: For i = 0 To 100
Comm1.Output = send 
call Timer1_Timer()
i = i + 1 
Next i 
If i = 100 Then 
j = 0 
GoTo BZ2 
End If BZ2: For j = 0 To 100
Comm1.Output = send 
call Timer1_Timer()
j = j + 1 
Next j 
If j = 100 Then 
i = 0 
GoTo BZ1 
End IfPrivate Sub Command2_Click()
exit sub
End SubPrivate Sub Timer1_Timer()
DoEvents
End Sub
这样子我能移交控制权给Command2吗?  现在就是程序一直在Command1里运行,怎么才能使Command2随时推出程序。

解决方案 »

  1.   


    private flag as booleanPrivate   Sub   Command1_Click()
    flag=false
    do
    ... //loop body
    if flag=true then exit sub
    DoEvents 
    end subPrivate   Sub   Command2_Click() 
    flag=true
    end subloop ...
      

  2.   

    最好还是定义一个模块级的变量Private m_Cancel As BooleanPrivate Sub Command1_Click()
        m_Cancel = False
        Do Until m_Cancel
            Comm1.Output = send
            DoEvents
        Loop
    End SubPrivate Sub Command2_Click()
        m_Cancel = True
    End Sub
    你的程序简化成这样应该没问题