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

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

public InitAndStartStopped ( System.Action Action ) : void
Action System.Action
Результат void
		public void InitAndStartStopped(Action Action)
		{
			this.Action = Action;
			this.ParentThread = Thread.CurrentThread;

			ParentEvent = new ManualResetEvent(false);
			ThisEvent = new ManualResetEvent(false);

			var This = this;

			this.CurrentThread = new Thread(() =>
			{
				ThisGreenThreadList.Value = This;
				ThisSemaphoreWaitOrParentThreadStopped();
				try
				{
					Running = true;
					Action();
				}
				catch (StopException)
				{
				}
				catch (Exception Exception)
				{
					RethrowException = Exception;
				}
				finally
				{
					Running = false;
					try
					{
						ParentEvent.Set();
					}
					catch
					{
					}
				}

				//Console.WriteLine("GreenThread.Running: {0}", Running);
			});

			this.CurrentThread.Name = "GreenThread-" + GreenThreadLastId++;

			this.CurrentThread.Start();
		}

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::InitAndStartStopped