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.
精彩评论