private void OpStateChange(OpStateFlag newOpState) { lock (opStateLock) { opState = newOpState; } }
public Modeling(int height, int width) { requests = new Queue(); opStateLock = new object(); nodes = new ArrayList(); updates = new Queue(); loopMin = 100; LoadNodesGrid(height, width); System.Random randGen = new Random(unchecked ((int)System.DateTime.Now.Ticks)); foreach (Node node in nodes) { //random activity node.IsActive = Convert.ToBoolean(randGen.Next(2)); } //activation values corresponding to Conway's Life this.activationValues = new ArrayList(new double[3] { 0.28D, 0.36D, 0.40D }); foreach (Node node in nodes) { updates.Enqueue(node); } modelingThread = new Thread( new ThreadStart(this.ModelingThreadEntryPoint) ); modelingThread.Name = "modelingThread"; opState = OpStateFlag.Starting; RequestPause(); modelingThread.Start(); }
private void ProcessRequests() { lock (requests) { //while OpState request has not been satisfied, it is possible //that while waiting in paused mode, we may be awoken to find //a new request or multiple requests may be made before //ProcessRequests is called. while (requests.Count > 0) { if (requests.Peek() is OpStateFlag) { OpStateFlag request = (OpStateFlag)requests.Dequeue(); switch (request) { case OpStateFlag.Paused: opState = OpStateFlag.Paused; //if there are still requests remaining, then //we should set state to pause, but continue //processing requests since there will likely //be requests for TimeStepping or Updating which //would normally cancel a pause. if (requests.Count == 0) { Monitor.Wait(requests); } break; case OpStateFlag.Updating: opState = OpStateFlag.Updating | opState; ProcessUpdate(); if (requests.Count == 0) { requests.Enqueue( (~OpStateFlag.Updating) & opState); break; } else { opState = ((~OpStateFlag.Updating) & opState); } break; case OpStateFlag.TimeStepping: opState = OpStateFlag.TimeStepping; break; case OpStateFlag.Exiting: opState = OpStateFlag.Exiting; break; default: Console.WriteLine ("Error, default reached in ProcessRequest"); break; } } //add else if statement for other object types in queue } } }