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