示例#1
0
        public void ManagedQueue_MultipleThreadsModifyEachOhersResources_DoesNotThrowExeption()
        {
            IEnumerable <int> iterations = Enumerable.Range(0, 10000000);

            ThreadDangerException exception = null;

            ThreadSafe threadSafe = new ThreadSafe();

            try
            {
                Parallel.ForEach(iterations, x => threadSafe.DoSomething());
            }
            catch (AggregateException aex)
            {
                foreach (var ex in aex.Flatten().InnerExceptions)
                {
                    if (!(ex is ThreadDangerException))
                    {
                        throw;
                    }

                    exception = (ThreadDangerException)ex;
                }
            }

            Assert.IsNull(exception);
        }
示例#2
0
        public void ManagedQueue_MultipleThreadsModifyEachOhersResources_ThrowsExeption()
        {
            ThreadDangerException exception = null;

            Action action = () =>
            {
                IEnumerable <int> iterations = Enumerable.Range(0, 10000000);

                ThreadDangerous threadDangerous = new ThreadDangerous();

                try
                {
                    Parallel.ForEach(iterations, x => threadDangerous.DoSomething());
                }
                catch (AggregateException aex)
                {
                    foreach (var ex in aex.Flatten().InnerExceptions)
                    {
                        if (!(ex is ThreadDangerException))
                        {
                            throw;
                        }

                        exception = (ThreadDangerException)ex;
                    }
                }
            };

            do
            {
                action();
            } while (exception == null);

            Assert.IsNotNull(exception);
            Assert.AreEqual("Not thread safe", exception.Message);
        }