[gitattributes] Do CRLF normalization on sln/proj files
[mono-project.git] / mcs / nunit24 / NUnitCore / core / TestRunnerThread.cs
blob26f46b5b98fe3df20a88e9a4be24f0537541ad98
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 // ****************************************************************
7 using System;
8 using System.Threading;
9 using System.Configuration;
10 using System.Collections.Specialized;
12 namespace NUnit.Core
14 /// <summary>
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.
18 /// </summary>
19 public class TestRunnerThread
21 #region Private Fields
23 /// <summary>
24 /// The Test runner to be used in running tests on the thread
25 /// </summary>
26 private TestRunner runner;
28 /// <summary>
29 /// The System.Threading.Thread created by the object
30 /// </summary>
31 private Thread thread;
33 /// <summary>
34 /// Collection of TestRunner settings from the config file
35 /// </summary>
36 private NameValueCollection settings;
38 /// <summary>
39 /// The EventListener interface to receive test events
40 /// </summary>
41 private NUnit.Core.EventListener listener;
43 /// <summary>
44 /// Array of test names for ues by the thread proc
45 /// </summary>
46 //private string[] testNames;
47 private ITestFilter filter;
49 /// <summary>
50 /// Array of returned results
51 /// </summary>
52 private TestResult[] results;
54 #endregion
56 #region Properties
58 /// <summary>
59 /// True if the thread is executing
60 /// </summary>
61 public bool IsAlive
63 get { return this.thread.IsAlive; }
66 /// <summary>
67 /// Array of returned results
68 /// </summary>
69 public TestResult[] Results
71 get { return results; }
74 #endregion
76 #region Constructor
78 public TestRunnerThread( TestRunner runner )
80 this.runner = 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 )
90 try
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 );
111 #endregion
113 #region Public Methods
115 public void Wait()
117 if ( this.thread.IsAlive )
118 this.thread.Join();
121 public void Cancel()
123 ThreadUtility.Kill(this.thread);
126 public void StartRun( EventListener listener )
128 StartRun( listener, TestFilter.Empty );
131 public void StartRun( EventListener listener, ITestFilter filter )
133 this.listener = listener;
134 this.filter = filter;
136 thread.Start();
139 #endregion
141 #region Thread Proc
142 /// <summary>
143 /// The thread proc for our actual test run
144 /// </summary>
145 private void TestRunnerThreadProc()
149 results = new TestResult[] { runner.Run(this.listener, this.filter) };
151 catch (Exception ex)
153 throw new ApplicationException("Exception in TestRunnerThread", ex);
156 #endregion