Task.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Linq;
  3. using System.ComponentModel;
  4. using GCHR.Model;
  5. using GCHR.View;
  6. namespace GCHR.Control.Tasks
  7. {
  8. abstract class Task
  9. {
  10. protected ThreadXData Data = ThreadXData.GetInstance();
  11. protected Konfiguration Config = Konfiguration.GetInstance();
  12. public int ID;
  13. private readonly string _name;
  14. public Ampel Ampel;
  15. public BackgroundWorker Worker;
  16. private int[] _warteliste;
  17. public static Task CreateInstance(int id, string name, RunWorkerCompletedEventHandler completed, int[] waitfor)
  18. {
  19. var currentTask = GetTask(id, name);
  20. currentTask.Worker.RunWorkerCompleted += completed;
  21. currentTask.SetWarteliste(waitfor);
  22. return currentTask;
  23. }
  24. private static Task GetTask(int id, string name)
  25. {
  26. switch (name)
  27. {
  28. case "Kontenrahmen":
  29. return new KontenrahmenAlt(id, name);
  30. case "UebersetzungSuSa":
  31. return new UebersetzungSuSa(id, name);
  32. case "UebersetzungStat":
  33. return new UebersetzungStat(id, name);
  34. case "ManuelleKonten":
  35. return new ManuelleKonten(id, name);
  36. case "DatenimportSuSa":
  37. return Datenimport.DatenimportSuSa(id, name);
  38. case "DatenimportStat":
  39. return Datenimport.DatenimportStat(id, name);
  40. case "Verarbeitung":
  41. return new Verarbeitung(id, name);
  42. case "Verrechnung":
  43. return new Verrechnung(id, name);
  44. case "Export":
  45. return new Export(id, name);
  46. default:
  47. return new DummyTask(id, name);
  48. }
  49. }
  50. protected Task(int id, string name)
  51. {
  52. ID = id;
  53. _name = name;
  54. Ampel = new Ampel();
  55. Worker = new BackgroundWorker
  56. {
  57. WorkerReportsProgress = true,
  58. WorkerSupportsCancellation = false
  59. };
  60. Worker.DoWork += DoWork;
  61. }
  62. private void DoWork(object sender, DoWorkEventArgs e)
  63. {
  64. ReportProgress(5);
  65. AufgabeAusfuehren();
  66. ReportProgress(100);
  67. }
  68. protected abstract void AufgabeAusfuehren();
  69. public void SetWarteliste(int[] waitfor)
  70. {
  71. _warteliste = waitfor;
  72. }
  73. public void WartelisteAktualisieren(int aufgabeId)
  74. {
  75. if (_warteliste == null) return;
  76. for (var i = 0; i < _warteliste.Count(); i++)
  77. {
  78. if (_warteliste[i] == aufgabeId)
  79. {
  80. _warteliste[i] = -1;
  81. }
  82. }
  83. }
  84. public override String ToString()
  85. {
  86. return _name;
  87. }
  88. public bool TryRun()
  89. {
  90. if (_warteliste == null)
  91. return false;
  92. for (var i = 0; i < _warteliste.Count(); i++)
  93. {
  94. if (_warteliste[i] != -1)
  95. return false;
  96. }
  97. _warteliste = null;
  98. Worker.RunWorkerAsync();
  99. return true;
  100. }
  101. public void RegisterAmpel(Ampel ampel)
  102. {
  103. Ampel = ampel;
  104. Worker.ProgressChanged += (s1, e1) => Ampel.ProgressChanged(e1.ProgressPercentage, e1.UserState);
  105. Ampel.Reset();
  106. }
  107. public void ReportProgress(int percentProgress)
  108. {
  109. Worker.ReportProgress(percentProgress);
  110. }
  111. public void ReportProgress(int percentProgress, object userState)
  112. {
  113. Worker.ReportProgress(percentProgress, userState);
  114. }
  115. }
  116. class DummyTask : Task
  117. {
  118. public DummyTask(int id, string name)
  119. : base(id, name) { }
  120. protected override void AufgabeAusfuehren()
  121. {
  122. return;
  123. }
  124. }
  125. }