如何实现忽略/重试/一个动作取消格局? [英] How to implement Ignore/Retry/Cancel pattern for an action?

查看:303
本文介绍了如何实现忽略/重试/一个动作取消格局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C#.NET 4.0开发的两层Windows的窗体应用程序。在这个应用程序,我读了文件内容,并创建数据的接入层实体的名单,并将其返回给GUI层中网格视图显示。在我目前的逻辑,如果该文件中的行中的一个已经损坏或坏格式(即我不能从它创建一个实体)或任何其他异常,我抛出一个异常并中止进程。现在,我怎么能实现一个忽略/重试/取消格局?我的意思是显示一个忽略/重试/取消对话框,做任何用户需要(如忽略当前行并转到下一行,取消该过程,或重试当前失败的行读)的最佳途径? ?是有它的任何模式

I have a two layered Windows-Forms application developed by C#.NET 4.0 . In this APP i read a file content and create a list of entities in Data-Access-Layer, and return it to GUI-Layer to show in a grid view. In my current logic, if one of the lines in the file has corrupted or bad-formatted (that i can't create an entity from it) or any other exceptions, i throw an exception and abort the process. Now, how can i implement an Ignore/Retry/Cancel pattern? I mean what is the best way to show an Ignore/Retry/Cancel dialog box and do whatever user wants (eg. ignore the current line and goto next line, cancel the process, or retry the current failed line read) ? is there any pattern for it?

推荐答案

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

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.

这篇关于如何实现忽略/重试/一个动作取消格局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆