-
Notifications
You must be signed in to change notification settings - Fork 15
/
ExecutionHelper.cs
70 lines (62 loc) · 2.12 KB
/
ExecutionHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// ***********************************************************************
// Assembly :
// Author : David McCarter
// Created : 01-21-2020
//
// Last Modified By : David McCarter
// Last Modified On : 11-11-2020
// ***********************************************************************
// <copyright file="ExecutionHelper.cs" company="David McCarter - dotNetTips.com">
// Copyright (c) David McCarter - dotNetTips.com. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using dotNetTips.Utility.Standard.Extensions;
using dotNetTips.Utility.Standard.OOP;
namespace dotNetTips.Utility.Standard
{
/// <summary>
/// Helper class for executing methods.
/// </summary>
public static class ExecutionHelper
{
/// <summary>
/// Progressive retry for a function call.
/// </summary>
/// <param name="operation">The operation to perform.</param>
/// <param name="retryCount">The retry count (default 3).</param>
/// <param name="retryWaitMilliseconds">The retry wait milliseconds (default 100).</param>
/// <returns>System.Int32.</returns>
public static int ProgressiveRetry(Action operation, byte retryCount = 3, int retryWaitMilliseconds = 100)
{
Encapsulation.TryValidateParam<ArgumentNullException>(operation != null);
Encapsulation.TryValidateParam<ArgumentOutOfRangeException>(retryCount > 0);
Encapsulation.TryValidateParam<ArgumentOutOfRangeException>(retryWaitMilliseconds > 0);
var attempts = 0;
do
{
try
{
attempts++;
#pragma warning disable CS8602 // Dereference of a possibly null reference.
operation();
#pragma warning restore CS8602 // Dereference of a possibly null reference.
return attempts;
}
catch (Exception ex)
{
// Using Exception since the actual exception is unknown beforehand.
if (attempts == retryCount)
{
throw;
}
Debug.WriteLine(ex.GetAllMessages());
Task.Delay(retryWaitMilliseconds * attempts).Wait();
}
} while (true);
}
}
}