mirror of
https://github.com/farcasclaudiu/SmartThreadPool.git
synced 2026-06-22 09:01:19 +03:00
Added ApartmentState to threads + tests
This commit is contained in:
@@ -109,6 +109,7 @@
|
||||
<Compile Include="AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="TestThreadApartmentState.cs" />
|
||||
<Compile Include="TestParallelMethods.cs" />
|
||||
<Compile Include="TestFalseFillStateWithParams.cs" />
|
||||
<Compile Include="TestFillStateWithParams.cs" />
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace Amib.Threading
|
||||
private bool _enableLocalPerformanceCounters;
|
||||
private string _threadPoolName = SmartThreadPool.DefaultThreadPoolName;
|
||||
|
||||
public STPStartInfo()
|
||||
public STPStartInfo()
|
||||
{
|
||||
_performanceCounterInstanceName = SmartThreadPool.DefaultPerformanceCounterInstanceName;
|
||||
_threadPriority = SmartThreadPool.DefaultThreadPriority;
|
||||
@@ -37,8 +37,10 @@ namespace Amib.Threading
|
||||
_enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters;
|
||||
_threadPoolName = stpStartInfo._threadPoolName;
|
||||
_areThreadsBackground = stpStartInfo.AreThreadsBackground;
|
||||
#if !(_SILVERLIGHT)
|
||||
_apartmentState = stpStartInfo._apartmentState;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get/Set the idle timeout in milliseconds.
|
||||
@@ -160,5 +162,23 @@ namespace Amib.Threading
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
// - Work items return result.
|
||||
// - Support waiting synchronization for multiple work items.
|
||||
// - Work items can be cancelled.
|
||||
// - Passage of the caller thread’s context to the thread in the pool.
|
||||
// - Passage of the caller thread’s context to the thread in the pool.
|
||||
// - Minimal usage of WIN32 handles.
|
||||
// - Minor bug fixes.
|
||||
// 26 Dec 2004 - Changes:
|
||||
@@ -159,13 +159,13 @@ namespace Amib.Threading
|
||||
/// </summary>
|
||||
public const ThreadPriority DefaultThreadPriority = ThreadPriority.Normal;
|
||||
|
||||
/// <summary>
|
||||
/// The default fill state with params. (false)
|
||||
|
||||
/// <summary>
|
||||
/// The default thread pool name. (SmartThreadPool)
|
||||
/// </summary>
|
||||
public const string DefaultThreadPoolName = "SmartThreadPool";
|
||||
|
||||
/// <summary>
|
||||
/// The default fill state with params. (false)
|
||||
/// It is relevant only to QueueWorkItem of Action<...>/Func<...>
|
||||
/// </summary>
|
||||
public const bool DefaultFillStateWithArgs = false;
|
||||
@@ -174,7 +174,16 @@ namespace Amib.Threading
|
||||
/// The default thread backgroundness. (true)
|
||||
/// </summary>
|
||||
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
|
||||
|
||||
#region Member Variables
|
||||
@@ -619,8 +628,16 @@ namespace Amib.Threading
|
||||
// Configure the new thread and start it
|
||||
workerThread.Name = "STP " + Name + " Thread #" + _threadCounter;
|
||||
workerThread.IsBackground = _stpStartInfo.AreThreadsBackground;
|
||||
|
||||
#if !(_SILVERLIGHT) && !(_WINDOWS_CE)
|
||||
if (_stpStartInfo.ApartmentState != ApartmentState.Unknown)
|
||||
{
|
||||
workerThread.SetApartmentState(_stpStartInfo.ApartmentState);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(_SILVERLIGHT)
|
||||
workerThread.Priority = _stpStartInfo.ThreadPriority;
|
||||
workerThread.Priority = _stpStartInfo.ThreadPriority;
|
||||
#endif
|
||||
workerThread.Start();
|
||||
++_threadCounter;
|
||||
|
||||
Reference in New Issue
Block a user