TimeoutHelper.cs 881 B

1234567891011121314151617181920212223242526272829303132333435
  1. namespace FNZCM.BlazorWasm.Helpers
  2. {
  3. public static class TimeoutHelper
  4. {
  5. public static void SetTimeout(int ms, params Func<Task>[] actions)
  6. {
  7. int index = 0;
  8. var t = new System.Timers.Timer(ms);
  9. t.Elapsed += async delegate
  10. {
  11. t.Stop();
  12. try
  13. {
  14. await actions[index]?.Invoke();
  15. }
  16. finally
  17. {
  18. ++index;
  19. if (index >= actions.Length)
  20. {
  21. t.Stop();
  22. t.Dispose();
  23. t = null;
  24. }
  25. else
  26. {
  27. t.Start();
  28. }
  29. }
  30. };
  31. t.Start();
  32. }
  33. }
  34. }