'这样可以解决你的问题了吗?Dim I As Double '条件参数,作为公用变量 Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Private Sub Form_Load() Command1.Caption = "开始" Command2.Caption = "暂停" Me.FontSize = 42 End SubPrivate Sub Command1_Click() I = Val(Command1.Tag) Do While I <= 200000 Cls Print I Sleep 150 DoEvents I = I + 1 Loop End Sub Private Sub Command2_Click() Command1.Tag = I '保存当前值 I = 200001 '跳出循环 End SubPrivate Sub Form_Unload(Cancel As Integer) End '以防关闭窗体时,循环尚在进行。 End Sub
Dim i As Integer Dim CU As Boolean Private Sub Command1_Click() CU = True Call WhileEx End SubSub WhileEx() Do While i <= 20000 If CU = False Then Exit Sub Text1.Text = i DoEvents i = i + 1 Loop End SubPrivate Sub Command2_Click() CU = False End Sub
Option ExplicitDim bStop As Boolean Dim bPause As BooleanPrivate Sub Command1_Click() '开始 bStop = False bPause = False While bStop = False 'DoSomething Caption = Timer DoEvents While bPause DoEvents Wend Wend
End SubPrivate Sub Command2_Click() '暂停 bPause = Not bPause End SubPrivate Sub Command3_Click() '停止 bStop = True End SubPrivate Sub Form_Unload(Cancel As Integer) bStop = True End Sub
用msgbox控制有碍于美观
如果不是必须,用timer控件可以美观和效果兼得,仅供参考
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Private Sub Form_Load()
Command1.Caption = "开始"
Command2.Caption = "暂停"
Me.FontSize = 42
End SubPrivate Sub Command1_Click()
I = Val(Command1.Tag)
Do While I <= 200000
Cls
Print I
Sleep 150
DoEvents
I = I + 1
Loop
End Sub
Private Sub Command2_Click()
Command1.Tag = I '保存当前值
I = 200001 '跳出循环
End SubPrivate Sub Form_Unload(Cancel As Integer)
End '以防关闭窗体时,循环尚在进行。
End Sub
Dim CU As Boolean
Private Sub Command1_Click()
CU = True
Call WhileEx
End SubSub WhileEx()
Do While i <= 20000
If CU = False Then Exit Sub
Text1.Text = i
DoEvents
i = i + 1
Loop
End SubPrivate Sub Command2_Click()
CU = False
End Sub
Dim bPause As BooleanPrivate Sub Command1_Click() '开始 bStop = False
bPause = False
While bStop = False
'DoSomething
Caption = Timer
DoEvents
While bPause
DoEvents
Wend
Wend
End SubPrivate Sub Command2_Click() '暂停
bPause = Not bPause
End SubPrivate Sub Command3_Click() '停止
bStop = True
End SubPrivate Sub Form_Unload(Cancel As Integer)
bStop = True
End Sub
你可以先声明一个布尔变量:b=true。
按下“暂停”时,置b=false,按下“继续”时,置b=true。
在你的while循环中加入DoEvents语句,随时监测b的值,如果b=false,转而调用另一个进程pause。
在进程pause 中也加入DoEvents语句,随时监测b的值,如果b=true,返回调用,继续while循环。
至于pause进程如何设计就是你的事了。
不知我的思路对你有没有帮助?
其他的都不行不能完全保留现场
如果加入sleep可能更好,释放cpu资源
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
.....
While bPause
Sleep 150
DoEvents
Wend
.....