/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
- using System.Threading;
- using NUnit.Framework;
-
- namespace Delta.Utilities.Helpers
- {
- /// <summary>
- /// Simple thread helper class
- /// </summary>
- public static class ThreadHelper
- {
- #region Delegates
- /// <summary>
- /// Run delegate with no parameters for the thread.
- /// </summary>
- public delegate void ThreadRunDelegate();
-
- /// <summary>
- /// Helper delegate for ThreadHelper.Start with parameter.
- /// This way you can pass any parameter to the new thread easily.
- /// </summary>
- /// <param name="param">Parameter to pass into run code delegate</param>
- public delegate void ThreadWithParameterDelegate(object param);
- #endregion
-
- #region Start (Static)
- /// <summary>
- /// Start a thread with the given threadRunCode. Using the RunDelegate
- /// from Delta.Base because we have the Delta assembly in here anyway.
- /// Pretty much just creates a thread and starts it :)
- /// </summary>
- /// <param name="threadRunCode">Thread Run Code</param>
- /// <returns>Started thread with given run code</returns>
- public static Thread Start(this ThreadRunDelegate threadRunCode)
- {
- Thread newThread = new Thread(new ThreadStart(threadRunCode));
-
- newThread.Start();
- return newThread;
- }
-
- /// <summary>
- /// Start a thread with the given threadRunCode. Using the RunDelegate
- /// from Delta.Base because we have the Delta assembly in here anyway.
- /// Pretty much just creates a thread and starts it :)
- /// </summary>
- /// <param name="threadName">Thread Name</param>
- /// <param name="threadRunCode">Thread Run Code</param>
- /// <returns>Started thread with the given name and run code</returns>
- public static Thread Start(string threadName,
- ThreadRunDelegate threadRunCode)
- {
- Thread newThread = new Thread(new ThreadStart(threadRunCode));
-
- newThread.Name = threadName;
- newThread.Start();
- return newThread;
- }
-
- /// <summary>
- /// Start a thread with the given threadRunCode and a parameter used for
- /// threadRunCode for starting. Using the RunDelegate from Delta.Base
- /// because we have the Delta assembly in here anyway.
- /// Pretty much just creates a thread and starts it :)
- /// </summary>
- /// <param name="threadRunCode">Thread Run Code</param>
- /// <param name="param">Parameter to pass to the run code</param>
- /// <returns>Started thread with the given run code and parameter</returns>
- public static Thread Start(this ThreadWithParameterDelegate threadRunCode,
- object param)
- {
- Thread newThread = new Thread(new ParameterizedThreadStart(
- threadRunCode));
-
- newThread.Start(param);
- return newThread;
- }
- #endregion
-
- /// <summary>
- /// Tests
- /// </summary>
- internal class ThreadHelperTests
- {
- #region Start (LongRunning)
- /// <summary>
- /// Start Thread. Note: Too slow for a dynamic unit test.
- /// </summary>
- [Test]
- [Category("LongRunning")]
- public static void Start()
- {
- int someNumber = 1;
- // Execute some delayed code
- Thread delayedCodeThread = ThreadHelper.Start(delegate
- {
- Thread.Sleep(1);
- someNumber++;
- });
-
- // Make sure we are still at 1 :)
- Assert.Equal(1, someNumber);
- // Wait a bit until delayedCodeThread is finished
- delayedCodeThread.Join();
-
- // Now someNumber should be 2
- Assert.Equal(2, someNumber);
- }
- #endregion
- }
- }
- }