【发布时间】:2015-02-18 09:59:33
【问题描述】:
所以我有一个子程序(由命令按钮触发),它运行一个看起来相当耗时的进程(5 到 20 秒之间,取决于机器和我们网络的合作程度)。为了让用户清楚他们看不到的事情正在发生,我将鼠标指针更改为沙漏,然后在子退出时将其改回,无论退出的原因是什么。
考虑到这一点,我的代码看起来像这样(说明性示例,不是实际代码):
Private Sub cmdDoTheThing_Click()
On Error GoTo Err_cmdDoTheThing
Screen.MousePointer = 11 'Hourglass/Equivalent
'Check all data is available to Do The Thing
If Not MyModule.ThingIsDoable(Me.PrimaryKey) Then
MsgBox "Cannot Do The Thing, more preliminary things must be done first."
GoTo Exit_cmdDoTheThing
End If
'Try to Do The Thing (function returns false on failure)
If Not MyModule.DoTheThing(Me.PrimaryKey) Then
MsgBox "Processing of The Thing failed."
GoTo Exit_cmdDoTheThing
End If
'...
'Stuff here I don't want to do if either of the above failed
'...
Exit_cmdDoTheThing:
Screen.MousePointer = 0 'Default mouse pointer
Exit Sub
Err_cmdDoTheThing:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_DoTheThing
End Sub
我不想在每个可能的退出点重复 Screen.MousePointer = 0,所以我认为 GoTo 将作为一个不错的快捷方式,因为无论如何都需要 Exit_cmdDoTheThing 标签来处理错误。
这是 GoTo 语句的有效用例吗?如果不是,是否有其他方法可以达到相同的结果?毕竟我不想要一个突然的raptor attack。
【问题讨论】: