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