CSharpUtils.Threading.GreenThread.SwitchTo C# (CSharp) Метод

SwitchTo() публичный Метод

Called from the caller thread. This will give the control to the green thread.
public SwitchTo ( ) : void
Результат void
		public void SwitchTo()
		{
			ParentThread = Thread.CurrentThread;
			ParentEvent.Reset();
			ThisEvent.Set();
			if (Kill) Thread.CurrentThread.Abort();
			//ThisSemaphoreWaitOrParentThreadStopped();
			ParentEvent.WaitOne();
			if (RethrowException != null)
			{
				try
				{
					//StackTraceUtils.PreserveStackTrace(RethrowException);
					throw (new GreenThreadException("GreenThread Exception", RethrowException));
					//throw (RethrowException);
				}
				finally
				{
					RethrowException = null;
				}
			}
		}

Usage Example

Пример #1
0
		public void Test1Test()
		{
			var Values = new List<int>();

			var Thread1 = new GreenThread();
			var Thread2 = new GreenThread();
			Thread1.InitAndStartStopped(() =>
			{
				Values.Add(1);
				GreenThread.Yield();
				Values.Add(3);
			});
			Thread2.InitAndStartStopped(() =>
			{
				Values.Add(2);
				GreenThread.Yield();
				Values.Add(4);
			});

			Thread.Sleep(20);
			// Inits stopped.
			Assert.AreEqual("[]", Values.ToJson());
			Thread1.SwitchTo();
			Assert.AreEqual("[1]", Values.ToJson());
			Thread2.SwitchTo();
			Assert.AreEqual("[1,2]", Values.ToJson());
			Thread.Sleep(20);
			Thread1.SwitchTo();
			Assert.AreEqual("[1,2,3]", Values.ToJson());
			Thread2.SwitchTo();
			Assert.AreEqual("[1,2,3,4]", Values.ToJson());
		}
All Usage Examples Of CSharpUtils.Threading.GreenThread::SwitchTo