Added ApartmentState to threads + tests

This commit is contained in:
Ami Bar
2012-07-06 16:32:25 +03:00
parent 4ba09bfa3f
commit 10633fdf9f
4 changed files with 93 additions and 8 deletions
+1
View File
@@ -109,6 +109,7 @@
<Compile Include="AssemblyInfo.cs"> <Compile Include="AssemblyInfo.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="TestThreadApartmentState.cs" />
<Compile Include="TestParallelMethods.cs" /> <Compile Include="TestParallelMethods.cs" />
<Compile Include="TestFalseFillStateWithParams.cs" /> <Compile Include="TestFalseFillStateWithParams.cs" />
<Compile Include="TestFillStateWithParams.cs" /> <Compile Include="TestFillStateWithParams.cs" />
+47
View File
@@ -0,0 +1,47 @@
using System.Threading;
using NUnit.Framework;
using Amib.Threading;
namespace SmartThreadPoolTests
{
/// <summary>
/// Summary description for TestThreadPriority.
/// </summary>
[TestFixture]
[Category("TestThreadApartment")]
public class TestThreadApartmentState
{
[Test]
public void TestSTA()
{
CheckApartmentState(ApartmentState.STA);
}
[Test]
public void TestMTA()
{
CheckApartmentState(ApartmentState.MTA);
}
private static void CheckApartmentState(ApartmentState requestApartmentState)
{
STPStartInfo stpStartInfo = new STPStartInfo();
stpStartInfo.ApartmentState = requestApartmentState;
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
IWorkItemResult<ApartmentState> wir = stp.QueueWorkItem(() => GetCurrentThreadApartmentState());
ApartmentState resultApartmentState = wir.GetResult();
stp.WaitForIdle();
Assert.AreEqual(requestApartmentState, resultApartmentState);
}
private static ApartmentState GetCurrentThreadApartmentState()
{
return Thread.CurrentThread.GetApartmentState();
}
}
}
+22 -2
View File
@@ -17,7 +17,7 @@ namespace Amib.Threading
private bool _enableLocalPerformanceCounters; private bool _enableLocalPerformanceCounters;
private string _threadPoolName = SmartThreadPool.DefaultThreadPoolName; private string _threadPoolName = SmartThreadPool.DefaultThreadPoolName;
public STPStartInfo() public STPStartInfo()
{ {
_performanceCounterInstanceName = SmartThreadPool.DefaultPerformanceCounterInstanceName; _performanceCounterInstanceName = SmartThreadPool.DefaultPerformanceCounterInstanceName;
_threadPriority = SmartThreadPool.DefaultThreadPriority; _threadPriority = SmartThreadPool.DefaultThreadPriority;
@@ -37,9 +37,11 @@ namespace Amib.Threading
_enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters; _enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters;
_threadPoolName = stpStartInfo._threadPoolName; _threadPoolName = stpStartInfo._threadPoolName;
_areThreadsBackground = stpStartInfo.AreThreadsBackground; _areThreadsBackground = stpStartInfo.AreThreadsBackground;
#if !(_SILVERLIGHT)
_apartmentState = stpStartInfo._apartmentState;
#endif
} }
/// <summary> /// <summary>
/// Get/Set the idle timeout in milliseconds. /// Get/Set the idle timeout in milliseconds.
/// If a thread is idle (starved) longer than IdleTimeout then it may quit. /// If a thread is idle (starved) longer than IdleTimeout then it may quit.
@@ -160,5 +162,23 @@ namespace Amib.Threading
{ {
return new STPStartInfo(this) { _readOnly = true }; return new STPStartInfo(this) { _readOnly = true };
} }
#if !(_SILVERLIGHT)
private ApartmentState _apartmentState = SmartThreadPool.DefaultApartmentState;
/// <summary>
/// Get/Set the apartment state of threads in the thread pool
/// </summary>
public ApartmentState ApartmentState
{
get { return _apartmentState; }
set
{
ThrowIfReadOnly();
_apartmentState = value;
}
}
#endif
} }
} }
+22 -5
View File
@@ -7,7 +7,7 @@
// - Work items return result. // - Work items return result.
// - Support waiting synchronization for multiple work items. // - Support waiting synchronization for multiple work items.
// - Work items can be cancelled. // - Work items can be cancelled.
// - Passage of the caller threads context to the thread in the pool. // - Passage of the caller threads context to the thread in the pool.
// - Minimal usage of WIN32 handles. // - Minimal usage of WIN32 handles.
// - Minor bug fixes. // - Minor bug fixes.
// 26 Dec 2004 - Changes: // 26 Dec 2004 - Changes:
@@ -159,13 +159,13 @@ namespace Amib.Threading
/// </summary> /// </summary>
public const ThreadPriority DefaultThreadPriority = ThreadPriority.Normal; public const ThreadPriority DefaultThreadPriority = ThreadPriority.Normal;
/// <summary>
/// The default fill state with params. (false)
/// <summary> /// <summary>
/// The default thread pool name. (SmartThreadPool) /// The default thread pool name. (SmartThreadPool)
/// </summary> /// </summary>
public const string DefaultThreadPoolName = "SmartThreadPool"; public const string DefaultThreadPoolName = "SmartThreadPool";
/// <summary>
/// The default fill state with params. (false)
/// It is relevant only to QueueWorkItem of Action<...>/Func<...> /// It is relevant only to QueueWorkItem of Action<...>/Func<...>
/// </summary> /// </summary>
public const bool DefaultFillStateWithArgs = false; public const bool DefaultFillStateWithArgs = false;
@@ -175,6 +175,15 @@ namespace Amib.Threading
/// </summary> /// </summary>
public const bool DefaultAreThreadsBackground = true; public const bool DefaultAreThreadsBackground = true;
#if !(_SILVERLIGHT)
/// <summary>
/// The default apartment state of a thread in the thread pool.
/// The default is ApartmentState.Unknown which means the STP will not
/// set the apartment of the thread. It will use the .NET default.
/// </summary>
public const ApartmentState DefaultApartmentState = ApartmentState.Unknown;
#endif
#endregion #endregion
#region Member Variables #region Member Variables
@@ -619,8 +628,16 @@ namespace Amib.Threading
// Configure the new thread and start it // Configure the new thread and start it
workerThread.Name = "STP " + Name + " Thread #" + _threadCounter; workerThread.Name = "STP " + Name + " Thread #" + _threadCounter;
workerThread.IsBackground = _stpStartInfo.AreThreadsBackground; workerThread.IsBackground = _stpStartInfo.AreThreadsBackground;
#if !(_SILVERLIGHT) && !(_WINDOWS_CE)
if (_stpStartInfo.ApartmentState != ApartmentState.Unknown)
{
workerThread.SetApartmentState(_stpStartInfo.ApartmentState);
}
#endif
#if !(_SILVERLIGHT) #if !(_SILVERLIGHT)
workerThread.Priority = _stpStartInfo.ThreadPriority; workerThread.Priority = _stpStartInfo.ThreadPriority;
#endif #endif
workerThread.Start(); workerThread.Start();
++_threadCounter; ++_threadCounter;