2
0

RunTask.dpr 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. program RunTask;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. Classes,
  6. Quick.Commons,
  7. Quick.Console,
  8. System.SysUtils,
  9. Quick.Threads;
  10. var
  11. currentdownload : Integer;
  12. begin
  13. try
  14. ReportMemoryLeaksOnShutdown := True;
  15. currentdownload := 0;
  16. TRunTask.Execute([currentdownload],False,
  17. procedure(task : ITask)
  18. var
  19. i : Integer;
  20. a : Integer;
  21. begin
  22. //simulate a download task with random fail
  23. task.Result := 500;
  24. for i := 0 to 10 do
  25. begin
  26. task['currentdownload'] := i*10;
  27. //task.Param[0] := i*10;
  28. cout('Downloading %d%%...',[i*10],etTrace);
  29. Sleep(100);
  30. a := i Div (Random(5));
  31. end;
  32. task['statuscode'] := 200;
  33. task.Result := 200;
  34. cout('executed thread',etSuccess);
  35. end)
  36. .SetParameter('statuscode',0,False)
  37. //.RetryForever
  38. .WaitAndRetry(5,250,2)
  39. //.WaitAndRetry([250,2000,10000])
  40. .OnRetry(
  41. procedure(task : ITask; aException : Exception; var vStopRetries : Boolean)
  42. begin
  43. coutFmt('Failed at %d%%. Retrying downloading (Status code: %d [%s])...',[task['currentdownload'].AsInteger,task.Result.AsInteger,aException.Message],etWarning);
  44. end)
  45. .OnException(
  46. procedure(task : ITask; aException : Exception)
  47. begin
  48. cout('Exception downloading %d%% : %s',[task['currentdownload'].AsInteger,aException.Message],etError);
  49. end)
  50. .OnTerminated(
  51. procedure(task : ITask)
  52. begin
  53. if task.Done then cout('Task finished ok',etSuccess)
  54. else coutFmt('Task failed after %d retries',[task.NumRetries],etError);
  55. cout('PRESS <ENTER> TO EXIT',etInfo);
  56. end)
  57. .Run;
  58. ConsoleWaitForEnterKey;
  59. except
  60. on E: Exception do
  61. Writeln(E.ClassName, ': ', E.Message);
  62. end;
  63. end.