各反復でアクション(PDF生成)を実行するループがあります
1回の繰り返しで(PDFの生成中に)例外が発生した場合、3回再試行したいと思います。次の反復/レコードに進む前に。
これを行う最善の方法は何ですか?
ありがとう。
各反復でアクション(PDF生成)を実行するループがあります
1回の繰り返しで(PDFの生成中に)例外が発生した場合、3回再試行したいと思います。次の反復/レコードに進む前に。
これを行う最善の方法は何ですか?
ありがとう。
簡単な方法の 1 つは、次のように、メイン ループ内でdo
/ネストされたループを使用し、そこで再試行をカウントすることです。while
foreach (var item in allReportingItems) {
var retries = 0;
var reportIsGenerated = false;
do {
reportIsGenerated = TryGeneratingReport(item);
retries++;
// The loop will end when the report gets generated
// or when the retries count would be exhausted
} while (!reportIsGenerated && retries < 3);
if (!reportIsGenerated) {
Concole.Error.WriteLine(
"Unable to generate report for {0} after 3 retries", item
);
}
}
基本的に、試行回数をカウントする for ループを持つことができます。
擬似コード:
for each action
success = false
for tries = 0; tries < 3 && !success; tries++
success = doAction(action)
または、例外を除いて:
for each action
success = false
for tries = 0; tries < 3 && !success; tries++
try
doAction(action)
success = true
catch exception
// do nothing