PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Helpers/ThreadHelper.cs

#
C# | 111 lines | 55 code | 11 blank | 45 comment | 0 complexity | e009efd49cc2e8c67456a4eb83b231d6 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Threading;
  2. using NUnit.Framework;
  3. namespace Delta.Utilities.Helpers
  4. {
  5. /// <summary>
  6. /// Simple thread helper class
  7. /// </summary>
  8. public static class ThreadHelper
  9. {
  10. #region Delegates
  11. /// <summary>
  12. /// Run delegate with no parameters for the thread.
  13. /// </summary>
  14. public delegate void ThreadRunDelegate();
  15. /// <summary>
  16. /// Helper delegate for ThreadHelper.Start with parameter.
  17. /// This way you can pass any parameter to the new thread easily.
  18. /// </summary>
  19. /// <param name="param">Parameter to pass into run code delegate</param>
  20. public delegate void ThreadWithParameterDelegate(object param);
  21. #endregion
  22. #region Start (Static)
  23. /// <summary>
  24. /// Start a thread with the given threadRunCode. Using the RunDelegate
  25. /// from Delta.Base because we have the Delta assembly in here anyway.
  26. /// Pretty much just creates a thread and starts it :)
  27. /// </summary>
  28. /// <param name="threadRunCode">Thread Run Code</param>
  29. /// <returns>Started thread with given run code</returns>
  30. public static Thread Start(this ThreadRunDelegate threadRunCode)
  31. {
  32. Thread newThread = new Thread(new ThreadStart(threadRunCode));
  33. newThread.Start();
  34. return newThread;
  35. }
  36. /// <summary>
  37. /// Start a thread with the given threadRunCode. Using the RunDelegate
  38. /// from Delta.Base because we have the Delta assembly in here anyway.
  39. /// Pretty much just creates a thread and starts it :)
  40. /// </summary>
  41. /// <param name="threadName">Thread Name</param>
  42. /// <param name="threadRunCode">Thread Run Code</param>
  43. /// <returns>Started thread with the given name and run code</returns>
  44. public static Thread Start(string threadName,
  45. ThreadRunDelegate threadRunCode)
  46. {
  47. Thread newThread = new Thread(new ThreadStart(threadRunCode));
  48. newThread.Name = threadName;
  49. newThread.Start();
  50. return newThread;
  51. }
  52. /// <summary>
  53. /// Start a thread with the given threadRunCode and a parameter used for
  54. /// threadRunCode for starting. Using the RunDelegate from Delta.Base
  55. /// because we have the Delta assembly in here anyway.
  56. /// Pretty much just creates a thread and starts it :)
  57. /// </summary>
  58. /// <param name="threadRunCode">Thread Run Code</param>
  59. /// <param name="param">Parameter to pass to the run code</param>
  60. /// <returns>Started thread with the given run code and parameter</returns>
  61. public static Thread Start(this ThreadWithParameterDelegate threadRunCode,
  62. object param)
  63. {
  64. Thread newThread = new Thread(new ParameterizedThreadStart(
  65. threadRunCode));
  66. newThread.Start(param);
  67. return newThread;
  68. }
  69. #endregion
  70. /// <summary>
  71. /// Tests
  72. /// </summary>
  73. internal class ThreadHelperTests
  74. {
  75. #region Start (LongRunning)
  76. /// <summary>
  77. /// Start Thread. Note: Too slow for a dynamic unit test.
  78. /// </summary>
  79. [Test]
  80. [Category("LongRunning")]
  81. public static void Start()
  82. {
  83. int someNumber = 1;
  84. // Execute some delayed code
  85. Thread delayedCodeThread = ThreadHelper.Start(delegate
  86. {
  87. Thread.Sleep(1);
  88. someNumber++;
  89. });
  90. // Make sure we are still at 1 :)
  91. Assert.Equal(1, someNumber);
  92. // Wait a bit until delayedCodeThread is finished
  93. delayedCodeThread.Join();
  94. // Now someNumber should be 2
  95. Assert.Equal(2, someNumber);
  96. }
  97. #endregion
  98. }
  99. }
  100. }