+
+ [TestFixture]
+ public class ThreadStateTest {
+ void Start ()
+ {
+ }
+
+ [Test] // bug #81720
+ public void IsBackGround ()
+ {
+ Thread t1 = new Thread (new ThreadStart (Start));
+ Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
+ Assert.IsFalse (t1.IsBackground, "#A2");
+ t1.Start ();
+ t1.Join ();
+ Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
+
+ try {
+ bool isBackGround = t1.IsBackground;
+ Assert.Fail ("#A4: " + isBackGround.ToString ());
+ } catch (ThreadStateException ex) {
+ Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
+ Assert.IsNull (ex.InnerException, "#A6");
+ Assert.IsNotNull (ex.Message, "#A7");
+ }
+
+ Thread t2 = new Thread (new ThreadStart (Start));
+ Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
+ t2.IsBackground = true;
+ Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
+ Assert.IsTrue (t2.IsBackground, "#B3");
+ t2.Start ();
+ t2.Join ();
+ Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
+
+ try {
+ bool isBackGround = t2.IsBackground;
+ Assert.Fail ("#B5: " + isBackGround.ToString ());
+ } catch (ThreadStateException ex) {
+ Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
+ Assert.IsNull (ex.InnerException, "#B7");
+ Assert.IsNotNull (ex.Message, "#B8");
+ }
+ }
+ }
+
+ [TestFixture]
+ public class ThreadApartmentTest
+ {
+ void Start ()
+ {
+ }
+
+ [Test] // bug #81658
+ public void ApartmentState_StoppedThread ()
+ {
+ Thread t1 = new Thread (new ThreadStart (Start));
+ t1.Start ();
+ t1.Join ();
+ try {
+ ApartmentState state = t1.ApartmentState;
+ Assert.Fail ("#A1: " + state.ToString ());
+ } catch (ThreadStateException ex) {
+ Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ }
+
+ Thread t2 = new Thread (new ThreadStart (Start));
+ t2.IsBackground = true;
+ t2.Start ();
+ t2.Join ();
+ try {
+ ApartmentState state = t2.ApartmentState;
+ Assert.Fail ("#B1: " + state.ToString ());
+ } catch (ThreadStateException ex) {
+ Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+ }
+ }
+
+ [Test]
+ public void ApartmentState_BackGround ()
+ {
+ Thread t1 = new Thread (new ThreadStart (Start));
+ t1.IsBackground = true;
+ Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
+ t1.ApartmentState = ApartmentState.STA;
+ Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
+ }
+
+ [Test]
+ public void TestApartmentState ()
+ {
+ Thread t1 = new Thread (new ThreadStart (Start));
+ Thread t2 = new Thread (new ThreadStart (Start));
+ Thread t3 = new Thread (new ThreadStart (Start));
+
+ Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
+ Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
+ Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
+
+ t1.ApartmentState = ApartmentState.STA;
+ Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
+ t1.ApartmentState = ApartmentState.MTA;
+ Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
+
+ t2.ApartmentState = ApartmentState.MTA;
+ Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
+ t2.ApartmentState = ApartmentState.STA;
+ Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
+
+ bool exception_occured = false;
+ try {
+ t3.ApartmentState = ApartmentState.Unknown;
+ }
+ catch (Exception) {
+ exception_occured = true;
+ }
+ Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
+#if NET_2_0
+ Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
+#else
+ Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
+#endif
+
+ t1.Start ();
+ exception_occured = false;
+ try {
+ t1.ApartmentState = ApartmentState.STA;
+ }
+ catch (Exception) {
+ exception_occured = true;
+ }
+ Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
+ }
+ }
+
+ public class TestUtil
+ {
+ public static void WaitForNotAlive (Thread t, string s)
+ {
+ WhileAlive (t, true, s);
+ }
+
+ public static void WaitForAlive (Thread t, string s)
+ {
+ WhileAlive (t, false, s);
+ }
+
+ public static bool WaitForAliveOrStop (Thread t, string s)
+ {
+ return WhileAliveOrStop (t, false, s);
+ }
+
+ public static void WhileAlive (Thread t, bool alive, string s)
+ {
+ DateTime ti = DateTime.Now;
+ while (t.IsAlive == alive) {
+ if ((DateTime.Now - ti).TotalSeconds > 10) {
+ if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
+ else Assert.Fail ("Timeout while waiting for alive state. " + s);
+ }
+ }
+ }
+
+ public static bool WhileAliveOrStop (Thread t, bool alive, string s)
+ {
+ DateTime ti = DateTime.Now;
+ while (t.IsAlive == alive) {
+ if (t.ThreadState == ThreadState.Stopped)
+ return false;
+
+ if ((DateTime.Now - ti).TotalSeconds > 10) {
+ if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
+ else Assert.Fail ("Timeout while waiting for alive state. " + s);
+ }
+ }
+
+ return true;
+ }
+ }