public void TestAppContextAcrossDifferentThreads() { List <AppContextThread> AppContextThreadList = new List <AppContextThread>(); List <Thread> ThreadList = new List <Thread>(); TestResults.Reinitialise(); for (int x = 0; x < 10; x++) { AppContextThread act = new AppContextThread("Thread: " + x); AppContextThreadList.Add(act); Thread t = new Thread(new ThreadStart(act.Run)); t.Name = "Thread: " + x; t.Start(); ThreadList.Add(t); } Exception ex = null; try { foreach (AppContextThread act in AppContextThreadList) { //We are accessing the Client/GlobalContext via this thread, therefore //it should be removed. Assert.AreEqual(true, act.Removed); } //We are now accessing the shared value. If any other thread //loses its Client/GlobalContext this will turn to true //Assert.AreEqual(false, AppContextThread.StaticRemoved); } catch (Exception e) { ex = e; } finally { foreach (AppContextThread act in AppContextThreadList) { act.Stop(); } foreach (Thread t in ThreadList) { t.Join(); } } if (ex != null) { throw ex; } }
public void TestAppContextAcrossDifferentThreads() { List<AppContextThread> AppContextThreadList = new List<AppContextThread>(); List<Thread> ThreadList = new List<Thread>(); for (int x = 0; x < 10; x++) { AppContextThread act = new AppContextThread("Thread: " + x); AppContextThreadList.Add(act); Thread t = new Thread(new ThreadStart(act.Run)); t.Name = "Thread: " + x; t.Start(); ThreadList.Add(t); } ApplicationContext.Clear(); Exception ex = null; try { foreach (AppContextThread act in AppContextThreadList) { //We are accessing the Client/GlobalContext via this thread, therefore //it should be removed. Assert.AreEqual(true, act.Removed); } //We are now accessing the shared value. If any other thread //loses its Client/GlobalContext this will turn to true //Assert.AreEqual(false, AppContextThread.StaticRemoved); } catch (Exception e) { ex = e; } finally { foreach (AppContextThread act in AppContextThreadList) act.Stop(); foreach (Thread t in ThreadList) { t.Join(); } } if (ex != null) throw ex; }