Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / corlib / Test / System.Threading / ThreadTest.cs
1 // ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
2 //
3 // Authors
4 //      Eduardo Garcia Cebollero (kiwnix@yahoo.es)
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) Eduardo Garcia Cebollero.
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using System;
13 using System.Globalization;
14 using System.Security.Principal;
15 using System.Threading;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System.Threading
20 {
21         // These tests seem to hang the 2.0 framework. So they are disabled for now
22         // Don't reenable them until you can run a few thousand times on an SMP box.
23         [Category ("NotWorking")]
24         public class ThreadedPrincipalTest
25         {
26                 public static void NoPrincipal () 
27                 {
28 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
29                         AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
30 #endif
31                         IPrincipal p = Thread.CurrentPrincipal;
32                         Assert.IsNull (p, "#1");
33
34                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
35                         Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
36
37                         Thread.CurrentPrincipal = null;
38                         Assert.IsNull (Thread.CurrentPrincipal, "#3");
39                         // in this case we can return to null
40                 }
41
42 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
43                 public static void UnauthenticatedPrincipal () 
44                 {
45                         AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
46                         IPrincipal p = Thread.CurrentPrincipal;
47                         Assert.IsNotNull (p, "#1");
48                         Assert.IsTrue ((p is GenericPrincipal), "#2");
49                         Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
50                         Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
51                         Assert.IsFalse (p.Identity.IsAuthenticated, "#5");
52
53                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
54                         Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
55
56                         Thread.CurrentPrincipal = null;
57                         Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
58                         // in this case we can't return to null
59                 }
60
61                 public static void WindowsPrincipal () 
62                 {
63                         AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
64                         IPrincipal p = Thread.CurrentPrincipal;
65                         Assert.IsNotNull (p, "#1");
66                         Assert.IsTrue ((p is WindowsPrincipal), "#2");
67                         Assert.IsNotNull (p.Identity.Name, "#3");
68                         Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
69                         Assert.IsTrue (p.Identity.IsAuthenticated, "#5");
70
71                         // note: we can switch from a WindowsPrincipal to a GenericPrincipal
72                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
73                         Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
74
75                         Thread.CurrentPrincipal = null;
76                         Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
77                         // in this case we can't return to null
78                 }
79 #endif // TARGET_JVM
80
81                 public static void CopyOnNewThread ()
82                 {
83                         Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
84                         Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
85                 }
86         }
87
88         [TestFixture]
89         [Category("MobileNotWorking")] // Abort #10240
90         public class ThreadTest
91         {
92                 TimeSpan Infinite = new TimeSpan (-10000);      // -10000 ticks == -1 ms
93                 TimeSpan SmallNegative = new TimeSpan (-2);     // between 0 and -1.0 (infinite) ms
94                 TimeSpan Negative = new TimeSpan (-20000);      // really negative
95                 TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
96                 TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
97
98                 static bool is_win32;
99                 static bool is_mono;
100
101                 static ThreadTest ()
102                 {
103                         switch (Environment.OSVersion.Platform) {
104                         case PlatformID.Win32NT:
105                         case PlatformID.Win32S:
106                         case PlatformID.Win32Windows:
107                         case PlatformID.WinCE:
108                                 is_win32 = true;
109                                 break;
110                         }
111
112                         // check a class in mscorlib to determine if we're running on Mono
113                         if (Type.GetType ("System.MonoType", false) != null)
114                                 is_mono = true;
115                 }
116
117                 //Some Classes to test as threads
118                 private class C1Test
119                 {
120                         public int cnt;
121                         public Thread thread1;
122                         public bool endm1;
123                         public bool endm2;
124
125                         public C1Test()
126                         {
127                                 thread1 = (Thread)null;
128                                 this.cnt = 0;
129                                 endm1 = endm2 = false;
130                         }
131                         
132                         public void TestMethod()
133                         {
134                                 while (cnt < 10)
135                                 {
136                                         cnt++;
137                                 }
138                                 endm1 = true;
139                         }
140                         public void TestMethod2()
141                         {
142                                 if (!(thread1==(Thread)null) )
143                                 {
144                                         thread1.Join();
145                                 }
146                                 endm2 = true;
147                         }
148                 }
149
150                 private class C2Test
151                 {
152                         public int cnt;
153                         public bool run = false;
154                         
155                         public C2Test()
156                         {
157                                 this.cnt = 0;
158                         }
159
160                         public void TestMethod()
161                         {
162                                 run = true;
163                                 while (true)
164                                 {
165                                         if (cnt < 1000)
166                                                 cnt++;
167                                         else
168                                                 cnt = 0;
169                                 }
170                         }
171                 }
172                 
173                 private class C3Test
174                 {
175                         public C1Test sub_class;
176                         public Thread sub_thread;
177
178                         public C3Test()
179                         {
180                                 sub_class = new C1Test();
181                                 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
182                         }
183
184                         public void TestMethod1()
185                         {
186                                 sub_thread.Start();
187                                 Thread.Sleep (100);
188                                 sub_thread.Abort();
189                         }
190                 }
191                 
192                 private class C4Test
193                 {
194                         public C1Test class1;
195                         public C1Test class2;
196                         public Thread thread1;
197                         public Thread thread2;
198                         public bool T1ON ;
199                         public bool T2ON ;
200
201                         public C4Test()
202                         {
203                                 T1ON = false;
204                                 T2ON = false;
205                                 class1 = new C1Test();
206                                 class2 = new C1Test();
207                                 thread1 = new Thread(new ThreadStart(class1.TestMethod));
208                                 thread2 = new Thread(new ThreadStart(class2.TestMethod));
209                         }
210
211                         public void TestMethod1()
212                         {
213                                 thread1.Start();
214                                 TestUtil.WaitForAlive (thread1, "wait1");
215                                 T1ON = true;
216                                 thread2.Start();
217                                 TestUtil.WaitForAlive (thread2, "wait2");
218                                 T2ON = true;
219                                 thread1.Abort();
220                                 TestUtil.WaitForNotAlive (thread1, "wait3");
221                                 T1ON = false;
222                                 thread2.Abort();
223                                 TestUtil.WaitForNotAlive (thread2, "wait4");
224                                 T2ON = false;
225                         }
226                         
227                         public void TestMethod2()
228                         {
229                                 thread1.Start();
230                                 thread1.Join();
231                         }
232                 }
233
234                 [Test]
235                 public void TestCtor1()
236                 {
237                         C1Test test1 = new C1Test();
238                         Thread t = new Thread (new ThreadStart (test1.TestMethod));
239
240                         Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
241                         Assert.IsFalse (t.IsAlive, "IsAlive");
242                         Assert.IsFalse (t.IsBackground, "IsBackground");
243                         Assert.IsNull (t.Name, "Name");
244                         Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
245                 }
246
247                 [Test]
248                 [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
249                 public void CultureInfo_Shared_Across_Threads ()
250                 {
251                         Thread t = new Thread (TestCtor1);
252                         Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");
253
254                         Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
255                         Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");
256
257                         Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
258                         Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
259                 }
260
261                 [Test] // bug #325566
262                 public void GetHashCodeTest ()
263                 {
264                         C1Test test1 = new C1Test ();
265                         Thread tA = new Thread (new ThreadStart (test1.TestMethod));
266                         int hA1 = tA.GetHashCode ();
267 #if NET_2_0
268                         Assert.IsTrue (hA1 > 0, "#A1");
269 #endif
270                         tA.Start ();
271                         int hA2 = tA.GetHashCode ();
272                         Assert.AreEqual (hA1, hA2, "#A2");
273                         tA.Join ();
274                         int hA3 = tA.GetHashCode ();
275                         Assert.AreEqual (hA1, hA3, "#A3");
276 #if NET_2_0
277                         Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
278 #endif
279
280                         test1 = new C1Test ();
281                         Thread tB = new Thread (new ThreadStart (test1.TestMethod));
282                         int hB1 = tB.GetHashCode ();
283 #if NET_2_0
284                         Assert.IsTrue (hB1 > 0, "#B1");
285 #endif
286                         tB.Start ();
287                         int hB2 = tB.GetHashCode ();
288                         Assert.AreEqual (hB1, hB2, "#B2");
289                         tB.Join ();
290                         int hB3 = tB.GetHashCode ();
291                         Assert.AreEqual (hB1, hB3, "#B3");
292 #if NET_2_0
293                         Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
294 #endif
295                         Assert.IsFalse (hA2 == hB2, "#B5");
296                 }
297
298 #if NET_2_0
299                 [Test] // bug #82700
300                 public void ManagedThreadId ()
301                 {
302                         C1Test test1 = new C1Test ();
303                         Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
304                         int mtA1 = t1.ManagedThreadId;
305                         t1.Start ();
306                         int mtA2 = t1.ManagedThreadId;
307                         t1.Join ();
308                         int mtA3 = t1.ManagedThreadId;
309                         Assert.AreEqual (mtA1, mtA2, "#A1");
310                         Assert.AreEqual (mtA2, mtA3, "#A2");
311
312                         test1 = new C1Test ();
313                         Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
314                         int mtB1 = t2.ManagedThreadId;
315                         t2.Start ();
316                         int mtB2 = t2.ManagedThreadId;
317                         t2.Join ();
318                         int mtB3 = t2.ManagedThreadId;
319                         Assert.AreEqual (mtB1, mtB2, "#B1");
320                         Assert.AreEqual (mtB2, mtB3, "#B2");
321                         Assert.IsFalse (mtB1 == mtA1, "#B3");
322                 }
323 #endif
324
325                 [Test]
326                 [Category ("NotDotNet")] // it hangs.
327                 public void TestStart()
328                 {
329                         if (is_win32 && is_mono)
330                                 Assert.Fail ("This test fails on Win32. The test should be fixed.");
331                 {
332                         C1Test test1 = new C1Test();
333                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
334                         TestThread.Start();
335                         TestThread.Join();
336                         Assert.AreEqual (10, test1.cnt, "#1");
337                 }
338                 {
339                         C2Test test1 = new C2Test();
340                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
341                         TestThread.Start();
342                         TestThread.Abort();
343                         try {
344                                 TestThread.Start();
345                                 Assert.Fail ("#2");
346                         } catch (ThreadStateException) {
347                         }
348                 }
349                 {
350                         C2Test test1 = new C2Test();
351                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
352                         TestThread.Start();
353                         while (!test1.run) {
354                         }
355                         bool started = (TestThread.ThreadState == ThreadState.Running);
356                         Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
357                         TestThread.Abort();
358                 }
359                 }
360
361                 [Test]
362                 public void TestApartmentState ()
363                 {
364                         if (is_win32 && is_mono)
365                                 Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
366
367                         C2Test test1 = new C2Test();
368                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
369                         Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
370                         TestThread.Start();
371                         TestUtil.WaitForAlive (TestThread, "wait5");
372 #if NET_2_0
373                         Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
374 #else
375                         Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
376 #endif
377                         TestThread.Abort();
378                 }
379
380                 [Test]
381                 public void TestPriority1()
382                 {
383                         if (is_win32 && is_mono)
384                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
385
386                         C2Test test1 = new C2Test();
387                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
388                         try {
389                                 TestThread.Priority=ThreadPriority.BelowNormal;
390                                 ThreadPriority after = TestThread.Priority;
391                                 TestThread.Start();
392                                 TestUtil.WaitForAlive (TestThread, "wait7");
393                                 ThreadPriority before = TestThread.Priority;
394                                 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
395                         } finally {
396                                 TestThread.Abort();
397                         }
398                 }
399
400                 [Test]
401                 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
402                 public void AbortUnstarted ()
403                 {
404                         C2Test test1 = new C2Test();
405                         Thread th = new Thread (new ThreadStart (test1.TestMethod));
406                         th.Abort ();
407                         th.Start ();
408                 }
409
410                 [Test]
411                 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
412                 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
413                 public void TestPriority2()
414                 {
415                         C2Test test1 = new C2Test();
416                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
417                         try {
418                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
419                                 TestThread.Start();
420                                 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
421                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
422                         } finally {
423                                 TestThread.Abort();
424                         }
425                         Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
426                 }
427
428                 [Test]
429                 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
430                 public void TestPriority3()
431                 {
432                         C2Test test1 = new C2Test();
433                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
434                         try {
435                                 TestThread.Start();
436                                 TestThread.Priority = ThreadPriority.Lowest;
437                                 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
438                                 TestThread.Priority = ThreadPriority.BelowNormal;
439                                 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
440                                 TestThread.Priority = ThreadPriority.Normal;
441                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
442                                 TestThread.Priority = ThreadPriority.AboveNormal;
443                                 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
444                                 TestThread.Priority = ThreadPriority.Highest;
445                                 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
446                         }
447                         finally {
448                                 TestThread.Abort();
449                         }
450                 }
451
452                 [Test]
453                 public void TestUndivisibleByPageSizeMaxStackSize ()
454                 {
455                         const int undivisible_stacksize = 1048573;
456
457                         var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
458                         thread.Start ();
459                         thread.Join ();
460                 }
461
462                 [Test]
463                 public void TestIsBackground1 ()
464                 {
465                         if (is_win32 && is_mono)
466                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
467
468                         C2Test test1 = new C2Test();
469                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
470                         try {
471                                 TestThread.Start();
472                                 TestUtil.WaitForAlive (TestThread, "wait9");
473                                 bool state = TestThread.IsBackground;
474                                 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
475                         } finally {
476                                 TestThread.Abort();
477                         }
478                 }
479
480                 [Test]
481                 public void TestIsBackground2 ()
482                 {
483                         C2Test test1 = new C2Test();
484                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
485                         TestThread.IsBackground = true;
486                         try {
487                                 TestThread.Start();
488                         } finally {
489                                 TestThread.Abort();
490                         }
491                         
492                         if (TestThread.IsAlive) {
493                                 try {
494                                         Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
495                                 } catch (ThreadStateException) {
496                                         // Ignore if thread died meantime
497                                 }
498                         }
499                 }
500
501                 [Test]
502                 public void TestName()
503                 {
504                         if (is_win32 && is_mono)
505                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
506
507                         C2Test test1 = new C2Test();
508                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
509                         try {
510                                 TestThread.Start();
511                                 TestUtil.WaitForAlive (TestThread, "wait10");
512                                 string name = TestThread.Name;
513                                 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
514                                 string newname = "Testing....";
515                                 TestThread.Name = newname;
516                                 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
517                         } finally {
518                                 TestThread.Abort();
519                         }
520                 }
521
522                 [Test]
523                 public void Name ()
524                 {
525                         Thread t = new Thread (new ThreadStart (Name));
526                         Assert.IsNull (t.Name, "Name-1");
527                         t.Name = null;
528                         Assert.IsNull (t.Name, "Name-2");
529                 }
530
531                 [Test]
532                 [ExpectedException (typeof (InvalidOperationException))]
533                 public void ReName ()
534                 {
535                         Thread t = new Thread (new ThreadStart (ReName));
536                         t.Name = "a";
537                         t.Name = "b";
538                 }
539
540                 [Test]
541                 public void TestNestedThreads1()
542                 {
543                         C3Test test1 = new C3Test();
544                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
545                         try {
546                                 TestThread.Start();
547                                 TestUtil.WaitForAlive (TestThread, "wait11");
548                         } finally {
549                                 TestThread.Abort();
550                         }
551                 }
552
553                 [Test]
554                 public void TestNestedThreads2()
555                 {
556                         C4Test test1 = new C4Test();
557                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
558                         try {
559                                 TestThread.Start();
560                         } finally {
561                                 TestThread.Abort();
562                         }
563                 }
564
565                 [Test]
566                 public void TestJoin1()
567                 {
568                         C1Test test1 = new C1Test();
569                         C1Test test2 = new C1Test();
570                         Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
571                         Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
572                         try {
573                                 thread1.Start();
574                                 thread2.Start();
575                                 thread2.Join();
576                         } finally {
577                                 thread1.Abort();
578                                 thread2.Abort();
579                         }
580                 }
581
582                 [Test]
583                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
584                 public void Join_Int32_Negative ()
585                 {
586                         // -1 is Timeout.Infinite
587                         Thread.CurrentThread.Join (-2);
588                 }
589
590                 [Test]
591                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
592                 public void Join_TimeSpan_Negative ()
593                 {
594                         Thread.CurrentThread.Join (Negative);
595                 }
596
597                 [Test]
598                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
599                 public void Join_TimeSpan_TooLarge ()
600                 {
601                         Thread.CurrentThread.Join (TooLarge);
602                 }
603
604                 [Test]
605                 public void Join_TimeSpan_SmallNegative ()
606                 {
607                         Thread.CurrentThread.Join (SmallNegative);
608                 }
609
610                 [Test]
611                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
612                 public void Sleep_Int32_Negative ()
613                 {
614                         // -1 is Timeout.Infinite
615                         Thread.Sleep (-2);
616                 }
617
618                 [Test]
619                 public void Sleep_TimeSpan_SmallNegative ()
620                 {
621                         Thread.Sleep (SmallNegative);
622                 }
623
624                 [Test]
625                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
626                 public void Sleep_TimeSpan_Negative ()
627                 {
628                         Thread.Sleep (Negative);
629                 }
630
631                 [Test]
632                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
633                 public void Sleep_TimeSpan_TooLarge ()
634                 {
635                         Thread.Sleep (TooLarge);
636                 }
637
638                 [Test]
639                 public void SpinWait ()
640                 {
641                         // no exception for negative numbers
642                         Thread.SpinWait (Int32.MinValue);
643                         Thread.SpinWait (0);
644                 }
645
646                 [Test]
647                 public void TestThreadState ()
648                 {
649                         if (is_win32 && is_mono)
650                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
651
652                         //TODO: Test The rest of the possible transitions
653                         C2Test test1 = new C2Test();
654                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
655                         Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
656                         try {
657                                 TestThread.Start();
658                                 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
659                                                                                           //but in the MS SDK it is
660                                 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
661                                         "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
662                         } finally {
663                                 TestThread.Abort();
664                         }
665                         
666                         TestUtil.WaitForNotAlive (TestThread, "wait12");
667                         // Docs say state will be Stopped, but Aborted happens sometimes (?)
668                         Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
669                                 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
670                 }
671
672                 [Test]
673                 [Ignore ("see comment below.")]
674                 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal () 
675                 {
676                         // note: switching from PrincipalPolicy won't work inside the same thread
677                         // because as soon as a Principal object is created the Policy doesn't matter anymore
678                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
679                         try {
680                                 t.Start ();
681                                 t.Join ();
682                         } catch {
683                                 t.Abort ();
684                         }
685                 }
686
687 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
688                 [Test]
689                 [Ignore ("see comment below.")]
690                 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal () 
691                 {
692                         // note: switching from PrincipalPolicy won't work inside the same thread
693                         // because as soon as a Principal object is created the Policy doesn't matter anymore
694                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
695                         try {
696                                 t.Start ();
697                                 t.Join ();
698                         } catch {
699                                 t.Abort ();
700                         }
701                 }
702
703                 [Test]
704                 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal () 
705                 {
706                         // note: switching from PrincipalPolicy won't work inside the same thread
707                         // because as soon as a Principal object is created the Policy doesn't matter anymore
708                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
709                         try {
710                                 t.Start ();
711                                 t.Join ();
712                         } catch {
713                                 t.Abort ();
714                         }
715                 }
716 #endif // TARGET_JVM
717                 
718                 [Test]
719                 public void IPrincipal_CopyOnNewThread () 
720                 {
721                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
722                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
723                         try {
724                                 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
725                                 t.Start ();
726                                 t.Join ();
727                         } catch {
728                                 t.Abort ();
729                         }
730                 }
731
732                 int counter = 0;
733
734                 [Test]
735                 public void TestSuspend ()
736                 {
737                         Thread t = new Thread (new ThreadStart (DoCount));
738                         t.IsBackground = true;
739                         t.Start ();
740                         
741                         CheckIsRunning ("t1", t);
742                         
743                         t.Suspend ();
744                         WaitSuspended ("t2", t);
745                         
746                         CheckIsNotRunning ("t3", t);
747                         
748                         t.Resume ();
749                         WaitResumed ("t4", t);
750                         
751                         CheckIsRunning ("t5", t);
752                         
753                         t.Abort ();
754                         TestUtil.WaitForNotAlive (t, "wait13");
755                         CheckIsNotRunning ("t6", t);
756                 }
757
758                 [Test]
759                 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
760                 public void TestSuspendAbort ()
761                 {
762                         if (is_win32 && is_mono)
763                                 Assert.Fail ("This test fails on Win32. The test should be fixed.");
764
765                         Thread t = new Thread (new ThreadStart (DoCount));
766                         t.IsBackground = true;
767                         t.Start ();
768                         
769                         CheckIsRunning ("t1", t);
770                         
771                         t.Suspend ();
772                         WaitSuspended ("t2", t);
773                         
774                         CheckIsNotRunning ("t3", t);
775                         
776                         t.Abort ();
777                         
778                         int n=0;
779                         while (t.IsAlive && n < 200) {
780                                 Thread.Sleep (10);
781                                 n++;
782                         }
783
784                         Assert.IsTrue (n < 200, "Timeout while waiting for abort");
785                         
786                         CheckIsNotRunning ("t6", t);
787                 }
788
789                 [Test]
790                 public void Test_Interrupt ()
791                 {
792                         bool interruptedExceptionThrown = false;
793                         ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
794
795                         try {
796                                 try {
797                                         Thread.Sleep (3000);
798                                 } finally {
799                                         try {
800                                                 Thread.Sleep (0);
801                                         } catch (ThreadInterruptedException) {
802                                                 Assert.Fail ("ThreadInterruptedException thrown twice");
803                                         }
804                                 }
805                         } catch (ThreadInterruptedException) {
806                                 interruptedExceptionThrown = true;
807                         }
808
809                         Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
810                 }
811
812                 [Test]
813                 [ExpectedException (typeof (ArgumentNullException))]
814                 public void TestQueueUserWorkItemNullCallback ()
815                 {
816                         ThreadPool.QueueUserWorkItem (null, null);
817                 }
818
819                 private void Test_Interrupt_Worker (object o)
820                 {
821                         Thread t = o as Thread;
822                         Thread.Sleep (100);
823                         t.Interrupt ();
824                 }
825                 
826                 [Test]
827                 [Category ("NotDotNet")] // it crashes nunit.
828                 public void Test_InterruptCurrentThread ()
829                 {
830                         bool interruptedExceptionThrown = false;
831
832                         Thread.CurrentThread.Interrupt ();
833                         try {
834                                 Thread.Sleep (0);
835                                 Assert.Fail ();
836                         } catch (ThreadInterruptedException) {
837                         }
838                 }
839
840                 [Test]
841                 public void GetNamedDataSlotTest ()
842                 {
843                         Assert.IsNotNull (Thread.GetNamedDataSlot ("te#st"), "#1");
844                         Assert.AreSame (Thread.GetNamedDataSlot ("te#st"), Thread.GetNamedDataSlot ("te#st"), "#2");
845                 }
846
847                 void CheckIsRunning (string s, Thread t)
848                 {
849                         int c = counter;
850                         Thread.Sleep (100);
851                         Assert.IsTrue (counter > c, s);
852                 }
853                 
854                 void CheckIsNotRunning (string s, Thread t)
855                 {
856                         int c = counter;
857                         Thread.Sleep (100);
858                         Assert.AreEqual (counter, c, s);
859                 }
860                 
861                 void WaitSuspended (string s, Thread t)
862                 {
863                         int n=0;
864                         ThreadState state = t.ThreadState;
865                         while ((state & ThreadState.Suspended) == 0) {
866                                 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
867                                 Thread.Sleep (10);
868                                 n++;
869                                 Assert.IsTrue (n < 100, s + ": failed to suspend");
870                                 state = t.ThreadState;
871                         }
872                         Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
873                 }
874                 
875                 void WaitResumed (string s, Thread t)
876                 {
877                         int n=0;
878                         while ((t.ThreadState & ThreadState.Suspended) != 0) {
879                                 Thread.Sleep (10);
880                                 n++;
881                                 Assert.IsTrue (n < 100, s + ": failed to resume");
882                         }
883                 }
884                 
885                 public void DoCount ()
886                 {
887                         while (true) {
888                                 counter++;
889                                 Thread.Sleep (1);
890                         }
891                 }
892         }
893
894         [TestFixture]
895         public class ThreadStateTest {
896                 void Start ()
897                 {
898                 }
899
900                 [Test] // bug #81720
901                 public void IsBackGround ()
902                 {
903                         Thread t1 = new Thread (new ThreadStart (Start));
904                         Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
905                         Assert.IsFalse (t1.IsBackground, "#A2");
906                         t1.Start ();
907                         t1.Join ();
908                         Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
909
910                         try {
911                                 bool isBackGround = t1.IsBackground;
912                                 Assert.Fail ("#A4: " + isBackGround.ToString ());
913                         } catch (ThreadStateException ex) {
914                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
915                                 Assert.IsNull (ex.InnerException, "#A6");
916                                 Assert.IsNotNull (ex.Message, "#A7");
917                         }
918
919                         Thread t2 = new Thread (new ThreadStart (Start));
920                         Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
921                         t2.IsBackground = true;
922                         Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
923                         Assert.IsTrue (t2.IsBackground, "#B3");
924                         t2.Start ();
925                         t2.Join ();
926                         Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
927
928                         try {
929                                 bool isBackGround = t2.IsBackground;
930                                 Assert.Fail ("#B5: " + isBackGround.ToString ());
931                         } catch (ThreadStateException ex) {
932                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
933                                 Assert.IsNull (ex.InnerException, "#B7");
934                                 Assert.IsNotNull (ex.Message, "#B8");
935                         }
936                 }
937         }
938
939         [TestFixture]
940         [Serializable]
941         public class ThreadTest_ManagedThreadId
942         {
943                 AppDomain ad1;
944                 AppDomain ad2;
945                 MBRO mbro = new MBRO ();
946
947                 class MBRO : MarshalByRefObject {
948                         public int id_a1;
949                         public int id_b1;
950                         public int id_b2;
951                         public string ad_a1;
952                         public string ad_b1;
953                         public string ad_b2;
954                         public string message;
955                 }
956 #if !MOBILE
957                 [Test]
958                 public void ManagedThreadId_AppDomains ()
959                 {
960                         AppDomain currentDomain = AppDomain.CurrentDomain;
961                         ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
962                         ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
963
964                         Thread a = new Thread (ThreadA);
965                         Thread b = new Thread (ThreadB);
966                         // execute on AppDomain 1 thread A
967                         // execute on AppDomain 2 thread B
968                         // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
969                         a.Start ();
970                         a.Join ();
971                         b.Start ();
972                         b.Join ();
973
974                         AppDomain.Unload (ad1);
975                         AppDomain.Unload (ad2);
976
977                         if (mbro.message != null)
978                                 Assert.Fail (mbro.message);
979
980                         // Console.WriteLine ("Done id_a1: {0} id_b1: {1} id_b2: {2} ad_a1: {3} ad_b1: {4} ad_b2: {5}", mbro.id_a1, mbro.id_b1, mbro.id_b2, mbro.ad_a1, mbro.ad_b1, mbro.ad_b2);
981
982                         Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
983                         Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
984                         Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
985
986                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
987                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
988                         Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
989
990                         Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
991                         Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
992                         Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
993                         Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
994                         Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
995                         Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
996                 }
997 #endif
998                 void A1 ()
999                 {
1000                         mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
1001                         mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
1002                 }
1003                 
1004                 void B2 ()
1005                 {
1006                         mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
1007                         mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1008                 }
1009
1010                 void B1 ()
1011                 {
1012                         mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1013                         mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1014                 }
1015
1016                 void ThreadA (object obj)
1017                 {
1018                         // Console.WriteLine ("ThreadA");
1019                         try {
1020                                 ad1.DoCallBack (A1);
1021                         } catch (Exception ex) {
1022                                 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1023                         }
1024                         // Console.WriteLine ("ThreadA Done");
1025                 }
1026
1027                 void ThreadB (object obj)
1028                 {
1029                         // Console.WriteLine ("ThreadB");
1030                         try {
1031                                 ad2.DoCallBack (B2);
1032                                 ad1.DoCallBack (B1);
1033                         } catch (Exception ex) {
1034                                 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1035                         }
1036                         // Console.WriteLine ("ThreadB Done");
1037                 }
1038         }
1039
1040         [TestFixture]
1041         public class ThreadApartmentTest
1042         {
1043                 void Start ()
1044                 {
1045                 }
1046
1047                 [Test] // bug #81658
1048                 public void ApartmentState_StoppedThread ()
1049                 {
1050                         Thread t1 = new Thread (new ThreadStart (Start));
1051                         t1.Start ();
1052                         t1.Join ();
1053                         try {
1054                                 ApartmentState state = t1.ApartmentState;
1055                                 Assert.Fail ("#A1: " + state.ToString ());
1056                         } catch (ThreadStateException ex) {
1057                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1058                                 Assert.IsNull (ex.InnerException, "#A3");
1059                                 Assert.IsNotNull (ex.Message, "#A4");
1060                         }
1061
1062                         Thread t2 = new Thread (new ThreadStart (Start));
1063                         t2.IsBackground = true;
1064                         t2.Start ();
1065                         t2.Join ();
1066                         try {
1067                                 ApartmentState state = t2.ApartmentState;
1068                                 Assert.Fail ("#B1: " + state.ToString ());
1069                         } catch (ThreadStateException ex) {
1070                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1071                                 Assert.IsNull (ex.InnerException, "#B3");
1072                                 Assert.IsNotNull (ex.Message, "#B4");
1073                         }
1074                 }
1075
1076                 [Test]
1077                 public void ApartmentState_BackGround ()
1078                 {
1079                         Thread t1 = new Thread (new ThreadStart (Start));
1080                         t1.IsBackground = true;
1081                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1082                         t1.ApartmentState = ApartmentState.STA;
1083                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1084                 }
1085
1086                 [Test]
1087                 public void TestApartmentState ()
1088                 {
1089                         Thread t1 = new Thread (new ThreadStart (Start));
1090                         Thread t2 = new Thread (new ThreadStart (Start));
1091                         Thread t3 = new Thread (new ThreadStart (Start));
1092
1093                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1094                         Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1095                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1096
1097                         t1.ApartmentState = ApartmentState.STA;
1098                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1099                         t1.ApartmentState = ApartmentState.MTA;
1100                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1101
1102                         t2.ApartmentState = ApartmentState.MTA;
1103                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1104                         t2.ApartmentState = ApartmentState.STA;
1105                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1106
1107                         bool exception_occured = false;
1108                         try {
1109                                 t3.ApartmentState = ApartmentState.Unknown;
1110                         }
1111                         catch (Exception) {
1112                                 exception_occured = true;
1113                         }
1114                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1115 #if NET_2_0
1116                         Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1117 #else
1118                         Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
1119 #endif
1120
1121                         t1.Start ();
1122                         exception_occured = false;
1123                         try {
1124                                 t1.ApartmentState = ApartmentState.STA;
1125                         }
1126                         catch (Exception) {
1127                                 exception_occured = true;
1128                         }
1129                         Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1130                 }
1131
1132                 [Test]
1133                 public void TestSetApartmentStateSameState ()
1134                 {
1135                         Thread t1 = new Thread (new ThreadStart (Start));
1136                         t1.SetApartmentState (ApartmentState.STA);
1137                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1138
1139                         t1.SetApartmentState (ApartmentState.STA);
1140                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
1141                 }
1142
1143                 [Test]
1144                 [ExpectedException(typeof(InvalidOperationException))]
1145                 public void TestSetApartmentStateDiffState ()
1146                 {
1147                         Thread t1 = new Thread (new ThreadStart (Start));
1148                         t1.SetApartmentState (ApartmentState.STA);
1149                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1150
1151                         t1.SetApartmentState (ApartmentState.MTA);
1152                 }
1153
1154                 [Test]
1155                 public void TestTrySetApartmentState ()
1156                 {
1157                         Thread t1 = new Thread (new ThreadStart (Start));
1158                         t1.SetApartmentState (ApartmentState.STA);
1159                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1160
1161                         bool result = t1.TrySetApartmentState (ApartmentState.MTA);
1162                         Assert.IsFalse (result, "#2");
1163
1164                         result = t1.TrySetApartmentState (ApartmentState.STA);
1165                         Assert.IsTrue (result, "#3");
1166                 }
1167
1168                 [Test]
1169                 public void TestTrySetApartmentStateRunning ()
1170                 {
1171                         Thread t1 = new Thread (new ThreadStart (Start));
1172                         t1.SetApartmentState (ApartmentState.STA);
1173                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1174
1175                         t1.Start ();
1176
1177                         try {
1178                                 t1.TrySetApartmentState (ApartmentState.STA);
1179                                 Assert.Fail ("#2");
1180                         } catch (ThreadStateException) {
1181                         }
1182
1183                         t1.Join ();
1184                 }
1185
1186                 [Test]
1187                 public void Volatile () {
1188                         double v3 = 55667;
1189                         Thread.VolatileWrite (ref v3, double.MaxValue);
1190                         Assert.AreEqual (v3, double.MaxValue);
1191
1192                         float v4 = 1;
1193                         Thread.VolatileWrite (ref v4, float.MaxValue);
1194                         Assert.AreEqual (v4, float.MaxValue);
1195                 }
1196
1197                 [Test]
1198                 public void Culture ()
1199                 {
1200                         Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
1201                         Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
1202                 }
1203
1204                 [Test]
1205                 public void ThreadStartSimple ()
1206                 {
1207                         int i = 0;
1208                         Thread t = new Thread (delegate () {
1209                                 // ensure the NSAutoreleasePool works
1210                                 i++;
1211                         });
1212                         t.Start ();
1213                         t.Join ();
1214                         Assert.AreEqual (1, i, "ThreadStart");
1215                 }
1216
1217                 [Test]
1218                 public void ParametrizedThreadStart ()
1219                 {
1220                         int i = 0;
1221                         object arg = null;
1222                         Thread t = new Thread (delegate (object obj) {
1223                                 // ensure the NSAutoreleasePool works
1224                                 i++;
1225                                 arg = obj;
1226                         });
1227                         t.Start (this);
1228                         t.Join ();
1229
1230                         Assert.AreEqual (1, i, "ParametrizedThreadStart");
1231                         Assert.AreEqual (this, arg, "obj");     
1232                 }               
1233
1234                 [Test]
1235                 public void SetNameTpThread () {
1236                         ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1237                 }
1238
1239                 static void ThreadProc(Object stateInfo) {
1240                         Thread.CurrentThread.Name = "My Worker";
1241                 }
1242         }
1243
1244         public class TestUtil
1245         {
1246                 public static void WaitForNotAlive (Thread t, string s)
1247                 {
1248                         WhileAlive (t, true, s);
1249                 }
1250                 
1251                 public static void WaitForAlive (Thread t, string s)
1252                 {
1253                         WhileAlive (t, false, s);
1254                 }
1255                 
1256                 public static bool WaitForAliveOrStop (Thread t, string s)
1257                 {
1258                         return WhileAliveOrStop (t, false, s);
1259                 }
1260                 
1261                 public static void WhileAlive (Thread t, bool alive, string s)
1262                 {
1263                         DateTime ti = DateTime.Now;
1264                         while (t.IsAlive == alive) {
1265                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1266                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1267                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1268                                 }
1269                         }
1270                 }
1271
1272                 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1273                 {
1274                         DateTime ti = DateTime.Now;
1275                         while (t.IsAlive == alive) {
1276                                 if (t.ThreadState == ThreadState.Stopped)
1277                                         return false;
1278
1279                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1280                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1281                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1282                                 }
1283                         }
1284
1285                         return true;
1286                 }
1287         }
1288 }