public void AbortWaitsUntilProtectedScopeEndsBeforeOccurring()
        {
            ThreadAbortScope scope = new ThreadAbortScope();

            ManualResetEvent barrier = new ManualResetEvent(false);
            Tasks.StartThreadTask("Background Abort", delegate
            {
                barrier.WaitOne();
                scope.Abort();
            });

            int count = 0;
            Assert.IsNotNull(scope.Run(delegate
            {
                count += 1;

                using (scope.Protect())
                {
                    count += 1;
                    barrier.Set();
                    Thread.Sleep(5000);
                    count += 1;
                }

                count += 1; // should not run
            }));

            Assert.AreEqual(3, count);

            Tasks.JoinAndVerify(TimeSpan.FromMilliseconds(100));
        }
        public void ProtectInAnotherThreadJustRunsTheAction()
        {
            ThreadAbortScope scope = new ThreadAbortScope();

            bool ran = false;

            Tasks.StartThreadTask("Different thread", () =>
            {
                using (scope.Protect())
                    ran = true;
            });
            Tasks.JoinAndVerify(TimeSpan.FromMilliseconds(100));

            Assert.IsTrue(ran, "Should have run the action.");
        }
        public void AbortFromWithinProtectedScopeDoesNotOccurUntilTheProtectedScopeExits()
        {
            bool ranToCompletion = false;
            ThreadAbortScope scope = new ThreadAbortScope();
            ThreadAbortException ex = scope.Run(() =>
            {
                using (scope.Protect())
                {
                    scope.Abort();
                    ranToCompletion = true;
                }
            });

            Assert.IsNotNull(ex, "Should have aborted.");
            Assert.IsTrue(ranToCompletion, "Should have run the action.");
        }
        public void ProtectOutsideOfScopeJustRunsTheAction()
        {
            ThreadAbortScope scope = new ThreadAbortScope();

            bool ran = false;
            using (scope.Protect())
                ran = true;

            Assert.IsTrue(ran, "Should have run the action.");
        }