Let’s say we copy some file using retry strategy (it might be blocked, etc.):
class Processor
{
public void CopyData() =>
CopyData(Try.Slow);
public void CopyData(Try loop) =>
loop.Execute(() =>
File.Copy(@"c\a.txt", @"c:\b.txt"));
}
What do you think about names chosen for the following library code identifiers? Would you name them differently?
public abstract class Try
{
public static Try Repeat(params int[] delays) => new Repeat(delays);
public static readonly Try Never = Repeat();
public static readonly Try Once = Repeat(0);
public static readonly Try Slow = Repeat(0, 500, 1500, 4500, 12000);
public static readonly Try Fast = Repeat(0, 50, 150, 450, 1200);
public abstract void Execute(Action action);
}
class Repeat : Try
{
IReadOnlyList<int> Delays { get; }
public Repeat(params int[] delays)
{
Delays = delays;
}
public override void Execute(Action action)
{
for(int i=0; i< Delays.Count; i++)
try
{
Thread.Sleep(Delays[i]);
action();
return;
}
catch
{
if (i == Delays.Count - 1)
throw;
}
}
}
Execute
(event based with optional cancellation) andExecuteAsync
version (Task.Delay with optional cancellation). We are still need to be synchronous sometimes. \$\endgroup\$ – Dmitry Nogin Sep 1 '16 at 16:53