开发者

How to implement Ignore/Retry/Cancel pattern for an action?

开发者 https://www.devze.com 2023-03-08 09:24 出处:网络
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 g

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 t开发者_Go百科he 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消