1 // ****************************************************************
2 // This is free software licensed under the NUnit license. You
3 // may obtain a copy of the license as well as information regarding
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
5 // ****************************************************************
8 using System
.Threading
;
9 using System
.Configuration
;
10 using System
.Collections
.Specialized
;
15 /// TestRunnerThread encapsulates running a test on a thread.
16 /// It knows how to create the thread based on configuration
17 /// settings and can cancel abort the test if necessary.
19 public class TestRunnerThread
21 #region Private Fields
24 /// The Test runner to be used in running tests on the thread
26 private TestRunner runner
;
29 /// The System.Threading.Thread created by the object
31 private Thread thread
;
34 /// Collection of TestRunner settings from the config file
36 private NameValueCollection settings
;
39 /// The EventListener interface to receive test events
41 private NUnit
.Core
.EventListener listener
;
44 /// Array of test names for ues by the thread proc
46 //private string[] testNames;
47 private ITestFilter filter
;
50 /// Array of returned results
52 private TestResult
[] results
;
59 /// True if the thread is executing
63 get { return this.thread.IsAlive; }
67 /// Array of returned results
69 public TestResult
[] Results
71 get { return results; }
78 public TestRunnerThread( TestRunner runner
)
81 this.thread
= new Thread( new ThreadStart( TestRunnerThreadProc
) );
82 thread
.IsBackground
= true;
83 thread
.Name
= "TestRunnerThread";
85 this.settings
= (NameValueCollection
)
86 ConfigurationSettings
.GetConfig( "NUnit/TestRunner" );
88 if ( settings
!= null )
92 string apartment
= settings
["ApartmentState"];
93 if ( apartment
!= null )
94 thread
.ApartmentState
= (ApartmentState
)
95 System
.Enum
.Parse( typeof( ApartmentState
), apartment
, true );
97 string priority
= settings
["ThreadPriority"];
98 if ( priority
!= null )
99 thread
.Priority
= (ThreadPriority
)
100 System
.Enum
.Parse( typeof( ThreadPriority
), priority
, true );
102 catch( ArgumentException ex
)
104 string msg
= string.Format( "Invalid configuration setting in {0}",
105 AppDomain
.CurrentDomain
.SetupInformation
.ConfigurationFile
);
106 throw new ArgumentException( msg
, ex
);
113 #region Public Methods
117 if ( this.thread
.IsAlive
)
123 this.thread
.Abort(); // Request abort first
125 // Wake up the thread if necessary
126 if ( ( this.thread
.ThreadState
& ThreadState
.WaitSleepJoin
) != 0 )
127 this.thread
.Interrupt();
130 public void StartRun( EventListener listener
)
132 StartRun( listener
, TestFilter
.Empty
);
135 public void StartRun( EventListener listener
, ITestFilter filter
)
137 this.listener
= listener
;
138 this.filter
= filter
;
147 /// The thread proc for our actual test run
149 private void TestRunnerThreadProc()
153 results
= new TestResult
[] { runner.Run(this.listener, this.filter) }
;
157 throw new ApplicationException("Exception in TestRunnerThread", ex
);