Guest User

Untitled

a guest
Jun 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. bool _run; //member of service class
  2.  
  3. //in OnStart
  4. _run = true;
  5.  
  6. //in a method on some other thread
  7. while ((yourLoopCondition) & _run)
  8. {
  9. //do stuff
  10. foreach (thing in things)
  11. {
  12. //do more stuff
  13. if (!_run) break;
  14. }
  15. }
  16. if (!_run) CleanUp();
  17.  
  18. //in OnStop
  19. _run = false;
  20.  
  21. // signal all threads to stop
  22. this.AllThreadsStopSignal.Set();
  23.  
  24. if (logThreads.IsDebugEnabled)
  25. logThreads.Debug ("Stop workers");
  26.  
  27. // remember the time of the signal
  28. DateTime signalTime = DateTime.Now;
  29.  
  30. // create an array of workers to be stopped
  31. List<IServiceWorker> workersToBeStopped = new List<IServiceWorker> (workers);
  32.  
  33. while (true)
  34. {
  35. // wait for some time
  36. Thread.Sleep (1000);
  37.  
  38. // go through the list and see if any workers have stopped
  39. int i = 0;
  40. while (i < workersToBeStopped.Count)
  41. {
  42. IServiceWorker workerToBeStopped = workersToBeStopped [i];
  43.  
  44. if (log.IsDebugEnabled)
  45. log.Debug (String.Format (System.Globalization.CultureInfo.InvariantCulture,
  46. "Stopping worker '{0}'. Worker state={1}",
  47. workerToBeStopped.WorkerDescription,
  48. workerToBeStopped.WorkerState));
  49.  
  50. bool stopped = workerToBeStopped.JoinThread (TimeSpan.Zero);
  51.  
  52. // if stopped, remove it from the list
  53. if (stopped)
  54. {
  55. workersToBeStopped.RemoveAt (i);
  56. if (log.IsDebugEnabled)
  57. log.Debug (String.Format (System.Globalization.CultureInfo.InvariantCulture,
  58. "Worker '{0}' was stopped.", workerToBeStopped.WorkerDescription));
  59. }
  60. else
  61. {
  62. i++;
  63. if (log.IsDebugEnabled)
  64. log.Debug (String.Format (System.Globalization.CultureInfo.InvariantCulture,
  65. "Worker '{0}' could not be stopped, will try again later. Worker state={1}",
  66. workerToBeStopped.WorkerDescription,
  67. workerToBeStopped.WorkerState));
  68. }
  69. }
  70.  
  71. // if all workers were stopped, exit from the loop
  72. if (workersToBeStopped.Count == 0)
  73. break;
  74.  
  75. // check if the duration of stopping has exceeded maximum time
  76. DateTime nowTime = DateTime.Now;
  77. TimeSpan duration = nowTime - signalTime;
  78.  
  79. if (duration > serviceCustomization.ThreadTerminationMaxDuration)
  80. {
  81. // execute forced abortion of all workers which have not stopped
  82. foreach (IServiceWorker worker in workersToBeStopped)
  83. {
  84. try
  85. {
  86. log.Warn (String.Format (System.Globalization.CultureInfo.InvariantCulture,
  87. "Aborting worker '{0}.", worker.WorkerDescription));
  88. worker.Abort ();
  89. log.Warn (String.Format (System.Globalization.CultureInfo.InvariantCulture,
  90. "Worker '{0}' aborted.", worker.WorkerDescription));
  91. }
  92. catch (ThreadStateException ex)
  93. {
  94. log.Warn (String.Format (System.Globalization.CultureInfo.InvariantCulture,
  95. "Worker '{0}' could not be aborted.", worker.WorkerDescription), ex);
  96. }
  97. }
  98. break;
  99. }
  100. }
Add Comment
Please, Sign In to add comment