Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. class Processor
  2. {
  3. public void CopyData() =>
  4. CopyData(Try.Slow);
  5.  
  6. public void CopyData(Try loop) =>
  7. loop.Execute(() =>
  8. File.Copy(@"ca.txt", @"c:b.txt"));
  9. }
  10.  
  11. public abstract class Try
  12. {
  13. public static Try Repeat(params int[] delays) => new Repeat(delays);
  14. public static readonly Try Never = Repeat();
  15. public static readonly Try Once = Repeat(0);
  16. public static readonly Try Slow = Repeat(0, 500, 1500, 4500, 12000);
  17. public static readonly Try Fast = Repeat(0, 50, 150, 450, 1200);
  18. public abstract void Execute(Action action);
  19. }
  20.  
  21. class Repeat : Try
  22. {
  23. IReadOnlyList<int> Delays { get; }
  24.  
  25. public Repeat(params int[] delays)
  26. {
  27. Delays = delays;
  28. }
  29.  
  30. public override void Execute(Action action)
  31. {
  32. for(int i=0; i< Delays.Count; i++)
  33. try
  34. {
  35. Thread.Sleep(Delays[i]);
  36. action();
  37. return;
  38. }
  39. catch
  40. {
  41. if (i == Delays.Count - 1)
  42. throw;
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement