ThreadTest.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. // ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
  2. //
  3. // Authors
  4. // Eduardo Garcia Cebollero ([email protected])
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) Eduardo Garcia Cebollero.
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // (C) 2004 Novell (http://www.novell.com)
  10. //
  11. using NUnit.Framework;
  12. using System;
  13. using System.Security.Principal;
  14. using System.Threading;
  15. namespace MonoTests.System.Threading {
  16. // These tests seem to hang the 2.0 framework. So they are disabled for now
  17. // Don't reenable them until you can run a few thousand times on an SMP box.
  18. [Category ("NotWorking")]
  19. public class ThreadedPrincipalTest : Assertion {
  20. public static void NoPrincipal ()
  21. {
  22. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  23. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
  24. #endif
  25. IPrincipal p = Thread.CurrentPrincipal;
  26. AssertNull ("Thread.CurrentPrincipal-1", p);
  27. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  28. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  29. Thread.CurrentPrincipal = null;
  30. AssertNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  31. // in this case we can return to null
  32. }
  33. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  34. public static void UnauthenticatedPrincipal ()
  35. {
  36. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
  37. IPrincipal p = Thread.CurrentPrincipal;
  38. AssertNotNull ("Thread.CurrentPrincipal", p);
  39. Assert ("Type", (p is GenericPrincipal));
  40. AssertEquals ("Name", String.Empty, p.Identity.Name);
  41. AssertEquals ("AuthenticationType", String.Empty, p.Identity.AuthenticationType);
  42. Assert ("IsAuthenticated", !p.Identity.IsAuthenticated);
  43. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  44. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  45. Thread.CurrentPrincipal = null;
  46. AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  47. // in this case we can't return to null
  48. }
  49. public static void WindowsPrincipal ()
  50. {
  51. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
  52. IPrincipal p = Thread.CurrentPrincipal;
  53. AssertNotNull ("Thread.CurrentPrincipal", p);
  54. Assert ("Type", (p is WindowsPrincipal));
  55. AssertNotNull ("Name", p.Identity.Name);
  56. AssertNotNull ("AuthenticationType", p.Identity.AuthenticationType);
  57. Assert ("IsAuthenticated", p.Identity.IsAuthenticated);
  58. // note: we can switch from a WindowsPrincipal to a GenericPrincipal
  59. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  60. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  61. Thread.CurrentPrincipal = null;
  62. AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  63. // in this case we can't return to null
  64. }
  65. #endif // TARGET_JVM
  66. public static void CopyOnNewThread ()
  67. {
  68. AssertNotNull ("Thread.CurrentPrincipal", Thread.CurrentPrincipal);
  69. AssertEquals ("Identity", "good", Thread.CurrentPrincipal.Identity.Name);
  70. }
  71. }
  72. [TestFixture]
  73. [Category ("NotWorking")]
  74. public class ThreadTest : Assertion {
  75. //Some Classes to test as threads
  76. private class C1Test
  77. {
  78. public int cnt;
  79. public Thread thread1;
  80. public bool endm1;
  81. public bool endm2;
  82. public C1Test()
  83. {
  84. thread1 = (Thread)null;
  85. this.cnt = 0;
  86. endm1 = endm2 = false;
  87. }
  88. public void TestMethod()
  89. {
  90. while (cnt < 10)
  91. {
  92. cnt++;
  93. }
  94. endm1 = true;
  95. }
  96. public void TestMethod2()
  97. {
  98. if (!(thread1==(Thread)null) )
  99. {
  100. thread1.Join();
  101. }
  102. endm2 = true;
  103. }
  104. }
  105. private class C2Test
  106. {
  107. public int cnt;
  108. public bool run = false;
  109. public C2Test()
  110. {
  111. this.cnt = 0;
  112. }
  113. public void TestMethod()
  114. {
  115. run = true;
  116. while (true)
  117. {
  118. if (cnt < 1000)
  119. cnt++;
  120. else
  121. cnt = 0;
  122. }
  123. }
  124. }
  125. private class C3Test
  126. {
  127. public C1Test sub_class;
  128. public Thread sub_thread;
  129. public C3Test()
  130. {
  131. sub_class = new C1Test();
  132. sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
  133. }
  134. public void TestMethod1()
  135. {
  136. sub_thread.Start();
  137. Thread.Sleep (100);
  138. sub_thread.Abort();
  139. }
  140. }
  141. private class C4Test
  142. {
  143. public C1Test class1;
  144. public C1Test class2;
  145. public Thread thread1;
  146. public Thread thread2;
  147. public bool T1ON ;
  148. public bool T2ON ;
  149. public C4Test()
  150. {
  151. T1ON = false;
  152. T2ON = false;
  153. class1 = new C1Test();
  154. class2 = new C1Test();
  155. thread1 = new Thread(new ThreadStart(class1.TestMethod));
  156. thread2 = new Thread(new ThreadStart(class2.TestMethod));
  157. }
  158. public void TestMethod1()
  159. {
  160. thread1.Start();
  161. TestUtil.WaitForAlive (thread1, "wait1");
  162. T1ON = true;
  163. thread2.Start();
  164. TestUtil.WaitForAlive (thread2, "wait2");
  165. T2ON = true;
  166. thread1.Abort();
  167. TestUtil.WaitForNotAlive (thread1, "wait3");
  168. T1ON = false;
  169. thread2.Abort();
  170. TestUtil.WaitForNotAlive (thread2, "wait4");
  171. T2ON = false;
  172. }
  173. public void TestMethod2()
  174. {
  175. thread1.Start();
  176. thread1.Join();
  177. }
  178. }
  179. public void TestCtor1()
  180. {
  181. C1Test test1 = new C1Test();
  182. try
  183. {
  184. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  185. }
  186. catch (Exception e)
  187. {
  188. Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
  189. }
  190. }
  191. [Category("NotDotNet")]
  192. public void TestStart()
  193. {
  194. {
  195. C1Test test1 = new C1Test();
  196. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  197. try
  198. {
  199. TestThread.Start();
  200. }
  201. catch (Exception e)
  202. {
  203. Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
  204. }
  205. TestThread.Join();
  206. AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
  207. }
  208. {
  209. bool errorThrown = false;
  210. C2Test test1 = new C2Test();
  211. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  212. TestThread.Start();
  213. TestThread.Abort();
  214. try
  215. {
  216. TestThread.Start();
  217. }
  218. catch(ThreadStateException)
  219. {
  220. errorThrown = true;
  221. }
  222. Assert ("#14 no ThreadStateException trown", errorThrown);
  223. }
  224. {
  225. C2Test test1 = new C2Test();
  226. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  227. TestThread.Start();
  228. while(!test1.run);
  229. bool started = (TestThread.ThreadState == ThreadState.Running);
  230. AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
  231. TestThread.Abort();
  232. }
  233. }
  234. public void TestApartment()
  235. {
  236. C2Test test1 = new C2Test();
  237. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  238. ApartmentState before = TestThread.ApartmentState;
  239. TestThread.Start();
  240. TestUtil.WaitForAlive (TestThread, "wait5");
  241. ApartmentState after = TestThread.ApartmentState;
  242. TestThread.Abort();
  243. AssertEquals("#21 Apartment State Changed when not needed",before,after);
  244. }
  245. [Category("NotDotNet")]
  246. public void TestApartmentState()
  247. {
  248. C2Test test1 = new C2Test();
  249. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  250. ApartmentState before = TestThread.ApartmentState;
  251. TestThread.Start();
  252. TestUtil.WaitForAlive (TestThread, "wait6");
  253. ApartmentState after = TestThread.ApartmentState;
  254. TestThread.Abort();
  255. AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
  256. }
  257. public void TestPriority1()
  258. {
  259. C2Test test1 = new C2Test();
  260. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  261. try {
  262. TestThread.Priority=ThreadPriority.BelowNormal;
  263. ThreadPriority after = TestThread.Priority;
  264. TestThread.Start();
  265. TestUtil.WaitForAlive (TestThread, "wait7");
  266. ThreadPriority before = TestThread.Priority;
  267. AssertEquals("#41 Unexpected Priority Change: ",before,after);
  268. }
  269. finally {
  270. TestThread.Abort();
  271. }
  272. }
  273. [Test]
  274. public void AbortUnstarted ()
  275. {
  276. C2Test test1 = new C2Test();
  277. Thread th = new Thread (new ThreadStart (test1.TestMethod));
  278. th.Abort ();
  279. th.Start ();
  280. }
  281. [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
  282. public void TestPriority2()
  283. {
  284. C2Test test1 = new C2Test();
  285. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  286. try {
  287. AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
  288. TestThread.Start();
  289. TestUtil.WaitForAliveOrStop (TestThread, "wait8");
  290. AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
  291. }
  292. finally {
  293. TestThread.Abort();
  294. }
  295. AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
  296. }
  297. [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
  298. public void TestPriority3()
  299. {
  300. C2Test test1 = new C2Test();
  301. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  302. try {
  303. TestThread.Start();
  304. TestThread.Priority = ThreadPriority.Lowest;
  305. AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
  306. TestThread.Priority = ThreadPriority.BelowNormal;
  307. AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
  308. TestThread.Priority = ThreadPriority.Normal;
  309. AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
  310. TestThread.Priority = ThreadPriority.AboveNormal;
  311. AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
  312. TestThread.Priority = ThreadPriority.Highest;
  313. AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
  314. }
  315. finally {
  316. TestThread.Abort();
  317. }
  318. }
  319. public void TestIsBackground1()
  320. {
  321. C2Test test1 = new C2Test();
  322. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  323. try {
  324. TestThread.Start();
  325. TestUtil.WaitForAlive (TestThread, "wait9");
  326. bool state = TestThread.IsBackground;
  327. Assert("#51 IsBackground not set at the default state: ",!(state));
  328. }
  329. finally {
  330. TestThread.Abort();
  331. }
  332. }
  333. public void TestIsBackground2()
  334. {
  335. C2Test test1 = new C2Test();
  336. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  337. TestThread.IsBackground = true;
  338. try {
  339. TestThread.Start();
  340. }
  341. finally {
  342. TestThread.Abort();
  343. }
  344. Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
  345. }
  346. public void TestName()
  347. {
  348. C2Test test1 = new C2Test();
  349. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  350. try {
  351. TestThread.Start();
  352. TestUtil.WaitForAlive (TestThread, "wait10");
  353. string name = TestThread.Name;
  354. AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
  355. string newname = "Testing....";
  356. TestThread.Name = newname;
  357. AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
  358. }
  359. finally {
  360. TestThread.Abort();
  361. }
  362. }
  363. [Category("NotDotNet")]
  364. public void TestNestedThreads1()
  365. {
  366. C3Test test1 = new C3Test();
  367. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  368. try {
  369. TestThread.Start();
  370. TestUtil.WaitForAlive (TestThread, "wait11");
  371. }
  372. catch(Exception e) {
  373. Fail("#71 Unexpected Exception" + e.Message);
  374. }
  375. finally {
  376. TestThread.Abort();
  377. }
  378. }
  379. public void TestNestedThreads2()
  380. {
  381. C4Test test1 = new C4Test();
  382. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  383. try {
  384. TestThread.Start();
  385. }
  386. catch(Exception e) {
  387. Fail("#81 Unexpected Exception" + e.ToString());
  388. }
  389. finally {
  390. TestThread.Abort();
  391. }
  392. }
  393. public void TestJoin1()
  394. {
  395. C1Test test1 = new C1Test();
  396. C1Test test2 = new C1Test();
  397. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  398. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  399. try
  400. {
  401. thread1.Start();
  402. thread2.Start();
  403. thread2.Join();
  404. }
  405. catch(Exception e)
  406. {
  407. Fail("#91 Unexpected Exception " + e.ToString());
  408. }
  409. finally
  410. {
  411. thread1.Abort();
  412. thread2.Abort();
  413. }
  414. }
  415. public void TestThreadState()
  416. {
  417. //TODO: Test The rest of the possible transitions
  418. C2Test test1 = new C2Test();
  419. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  420. AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
  421. try {
  422. TestThread.Start();
  423. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  424. //but in the MS SDK it is
  425. Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
  426. }
  427. finally {
  428. TestThread.Abort();
  429. }
  430. TestUtil.WaitForNotAlive (TestThread, "wait12");
  431. // Docs say state will be Stopped, but Aborted happens sometimes (?)
  432. Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
  433. || (ThreadState.Aborted & TestThread.ThreadState) != 0);
  434. }
  435. [Test]
  436. [Ignore ("see comment below.")]
  437. public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
  438. {
  439. // note: switching from PrincipalPolicy won't work inside the same thread
  440. // because as soon as a Principal object is created the Policy doesn't matter anymore
  441. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
  442. try {
  443. t.Start ();
  444. t.Join ();
  445. }
  446. catch {
  447. t.Abort ();
  448. }
  449. }
  450. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  451. [Test]
  452. [Ignore ("see comment below.")]
  453. public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
  454. {
  455. // note: switching from PrincipalPolicy won't work inside the same thread
  456. // because as soon as a Principal object is created the Policy doesn't matter anymore
  457. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
  458. try {
  459. t.Start ();
  460. t.Join ();
  461. }
  462. catch {
  463. t.Abort ();
  464. }
  465. }
  466. [Test]
  467. public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
  468. {
  469. // note: switching from PrincipalPolicy won't work inside the same thread
  470. // because as soon as a Principal object is created the Policy doesn't matter anymore
  471. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
  472. try {
  473. t.Start ();
  474. t.Join ();
  475. }
  476. catch {
  477. t.Abort ();
  478. }
  479. }
  480. #endif // TARGET_JVM
  481. [Test]
  482. public void IPrincipal_CopyOnNewThread ()
  483. {
  484. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
  485. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
  486. try {
  487. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
  488. t.Start ();
  489. t.Join ();
  490. }
  491. catch {
  492. t.Abort ();
  493. }
  494. }
  495. int counter = 0;
  496. [Category("NotDotNet")]
  497. public void TestSuspend ()
  498. {
  499. Thread t = new Thread (new ThreadStart (DoCount));
  500. t.IsBackground = true;
  501. t.Start ();
  502. CheckIsRunning ("t1", t);
  503. t.Suspend ();
  504. WaitSuspended ("t2", t);
  505. CheckIsNotRunning ("t3", t);
  506. t.Resume ();
  507. WaitResumed ("t4", t);
  508. CheckIsRunning ("t5", t);
  509. t.Abort ();
  510. TestUtil.WaitForNotAlive (t, "wait13");
  511. CheckIsNotRunning ("t6", t);
  512. }
  513. [Category("NotDotNet")]
  514. [Category("NotWorking")]
  515. public void TestSuspendAbort ()
  516. {
  517. Thread t = new Thread (new ThreadStart (DoCount));
  518. t.IsBackground = true;
  519. t.Start ();
  520. CheckIsRunning ("t1", t);
  521. t.Suspend ();
  522. WaitSuspended ("t2", t);
  523. CheckIsNotRunning ("t3", t);
  524. t.Abort ();
  525. int n=0;
  526. while (t.IsAlive && n < 200) {
  527. Thread.Sleep (10);
  528. n++;
  529. }
  530. Assert ("Timeout while waiting for abort", n < 200);
  531. CheckIsNotRunning ("t6", t);
  532. }
  533. void CheckIsRunning (string s, Thread t)
  534. {
  535. int c = counter;
  536. Thread.Sleep (100);
  537. Assert (s, counter > c);
  538. }
  539. void CheckIsNotRunning (string s, Thread t)
  540. {
  541. int c = counter;
  542. Thread.Sleep (100);
  543. Assert (s, counter == c);
  544. }
  545. void WaitSuspended (string s, Thread t)
  546. {
  547. int n=0;
  548. ThreadState state = t.ThreadState;
  549. while ((state & ThreadState.Suspended) == 0) {
  550. Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
  551. Thread.Sleep (10);
  552. n++;
  553. Assert (s + ": failed to suspend", n < 100);
  554. state = t.ThreadState;
  555. }
  556. Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
  557. }
  558. void WaitResumed (string s, Thread t)
  559. {
  560. int n=0;
  561. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  562. Thread.Sleep (10);
  563. n++;
  564. Assert (s + ": failed to resume", n < 100);
  565. }
  566. }
  567. public void DoCount ()
  568. {
  569. while (true) {
  570. counter++;
  571. Thread.Sleep (1);
  572. }
  573. }
  574. }
  575. public class TestUtil
  576. {
  577. public static void WaitForNotAlive (Thread t, string s)
  578. {
  579. WhileAlive (t, true, s);
  580. }
  581. public static void WaitForAlive (Thread t, string s)
  582. {
  583. WhileAlive (t, false, s);
  584. }
  585. public static bool WaitForAliveOrStop (Thread t, string s)
  586. {
  587. return WhileAliveOrStop (t, false, s);
  588. }
  589. public static void WhileAlive (Thread t, bool alive, string s)
  590. {
  591. DateTime ti = DateTime.Now;
  592. while (t.IsAlive == alive) {
  593. if ((DateTime.Now - ti).TotalSeconds > 10) {
  594. if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
  595. else Assertion.Fail ("Timeout while waiting for alive state. " + s);
  596. }
  597. }
  598. }
  599. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  600. {
  601. DateTime ti = DateTime.Now;
  602. while (t.IsAlive == alive) {
  603. if (t.ThreadState == ThreadState.Stopped)
  604. return false;
  605. if ((DateTime.Now - ti).TotalSeconds > 10) {
  606. if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
  607. else Assertion.Fail ("Timeout while waiting for alive state. " + s);
  608. }
  609. }
  610. return true;
  611. }
  612. }
  613. }