public ExecState(Action <object, ContextSwitcher> action, ContextSwitcher switcher, object state, Thread thread)
 {
     Action      = action;
     Switcher    = switcher;
     State       = state;
     this.Thread = thread;
 }
        public void CallMultiple(Action <Object, ContextSwitcher> act, Int32 count, Object[] state)
        {
            ExecutingCount = count;
            ContextSwitcherHost host = new ContextSwitcherHost(count);

            ContextSwitcher[] switchers = new ContextSwitcher[count];
            for (Int32 I = count - 1; I >= 0; --I)
            {
                switchers[I] = new ContextSwitcher(I);
                if (I < count - 1)
                {
                    switchers[I].NextSwitcher = switchers[I + 1];
                }
            }
            switchers[count - 1].NextSwitcher = switchers[0];
            for (Int32 I = 0; I < count; ++I)
            {
                Thread t = new Thread(CallerFunction);
                t.Name = "ParallelFunctionTester [" + I + "]";
                t.Start(new ExecState(act, switchers[I], state[I], t));
            }
            switchers[0].Signal();
            while (ExecutingCount > 0)
            {
                Thread.Sleep(300);
            }

            //Now raise the exception if needed
            StringBuilder sb     = new StringBuilder();
            StringBuilder detail = new StringBuilder();

            for (Int32 I = 0; I < count; ++I)
            {
                if (switchers[I].AssertionFail)
                {
                    sb.AppendFormat("Function number {0} has failed:\n", I);
                    detail.AppendFormat("Message for function number {0}:{1}\n", I, switchers[I].FailedAssertion);
                }
            }
            if (sb.Length > 0)
            {
                Assert.Fail(sb + "\n" + detail);
            }
        }
        public void CallMultiple(Action<Object, ContextSwitcher> act, Int32 count, Object[] state)
        {
            ExecutingCount = count;
            ContextSwitcherHost host = new ContextSwitcherHost(count);
            ContextSwitcher[] switchers = new ContextSwitcher[count];
            for (Int32 I = count - 1; I >= 0; --I)
            {
                switchers[I] = new ContextSwitcher(I);
                if (I < count - 1)
                {
                    switchers[I].NextSwitcher = switchers[I + 1];
                }
            }
            switchers[count - 1].NextSwitcher = switchers[0];
            for (Int32 I = 0; I < count; ++I)
            {
                Thread t = new Thread(CallerFunction);
                t.Name = "ParallelFunctionTester [" + I + "]";
                t.Start(new ExecState(act, switchers[I], state[I], t));
            }
            switchers[0].Signal();
            while (ExecutingCount > 0) Thread.Sleep(300);

            //Now raise the exception if needed
            StringBuilder sb = new StringBuilder();
            StringBuilder detail = new StringBuilder();
            for (Int32 I = 0; I < count; ++I)
            {
                if (switchers[I].AssertionFail)
                {
                    sb.AppendFormat("Function number {0} has failed:\n", I);
                    detail.AppendFormat("Message for function number {0}:{1}\n", I, switchers[I].FailedAssertion);
                }
            }
            if (sb.Length > 0) Assert.Fail(sb + "\n" + detail);
        }
 private void FunctionAssertFalse(Object state, ContextSwitcher switcher)
 {
     Assert.Fail("This is a failure");
 }
 public ExecState(Action<object, ContextSwitcher> action, ContextSwitcher switcher, object state, Thread thread)
 {
     Action = action;
     Switcher = switcher;
     State = state;
     this.Thread = thread;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="state"></param>
 /// <param name="switcher"></param>
 private void FunctionForThread(Object state, ContextSwitcher switcher)
 {
     Int32 value = retStatus;
     ++retStatus; // add 1
     switcher.Switch(); // I ask for switch, the next function should be executed so another 1 is added
     Assert.That(retStatus, Is.EqualTo(value + 2));
     ++retStatus;
 }
 private void FunctionWaitAndSet(Object state, ContextSwitcher switcher)
 {
     Thread.Sleep(200);
     retStatus++;
 }
 private void FunctionDumpWithFailureAssertion(Object state, ContextSwitcher switcher)
 {
     if (((String)state) == "C") Assert.Fail();
     trace.Append((String)state);
 }
 private void FunctionDumpThatThrowsException(Object state, ContextSwitcher switcher)
 {
     if (((String)state) == "C") throw new System.SystemException();
     trace.Append((String)state);
 }
 private void FunctionDumpSimple(Object state, ContextSwitcher switcher)
 {
     trace.Append((String) state);
 }
 private void FunctionDumpDouble(Object state, ContextSwitcher switcher)
 {
     trace.Append((String)state);
     switcher.Switch();
     trace.Append((String)state);
 }
 private void FunctionDoNothing(Object state, ContextSwitcher switcher)
 {
     //donothing
 }