PageRenderTime 167ms CodeModel.GetById 101ms app.highlight 14ms RepoModel.GetById 46ms app.codeStats 0ms

/Utilities/Helpers/ThreadHelper.cs

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