且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何实现忽略/重试/一个动作取消格局?

更新时间:2022-05-10 22:06:55

我采取以下方式提供这样的选项时:

I adopt the following pattern when offering such options:

DialogResult result = DialogResult.Retry;
while (result == DialogResult.Retry) {
    try {
        DoProcess();
        break;
    }
    catch {
        result = MessageBox.Show(errorMessage, caption, MessageBoxButtons.AbortRetryIgnore);
        if (result == DialogResult.Abort) throw;
    }
}

如果用户选择重试,循环会再次运行。如果用户点击中止时,将引发异常(待进一步赶上链,从而中止操作的其余部分)。忽略会导致循环退出而不抛出异常。我想不出这样做的更简洁的方式。

If the user selects Retry, the loop will run again. If the user clicks Abort, the exception will be thrown (to be caught further up the chain and thus abort the rest of the operation). Ignore will cause the loop to exit without throwing an exception. I can't think of a more concise way of doing this.