// Update is called once per frame void Update() { if (multithreaded) { bool startThread = buildThread == null; if (!startThread) { startThread = !buildThread.IsAlive; } if (startThread) { if (buildThread != null) { Debug.Log("Thread restarted"); } buildThread = new Thread(BuildThread); buildThread.Start(); } } if (postBuild) { buildSegment.OnPostExtrude(); buildSegment = null; postBuild = false; framesPassed = 0; } if (buildSegment == null && framesPassed >= framesBetweenBuilds) { if (buildQueue.Count > 0) { if (LevelSegment.generationState == LevelSegment.GenerationState.Free || LevelSegment.generationState == LevelSegment.GenerationState.Idle) { buildSegment = buildQueue[0]; buildSegment.OnBeforeExtrude(); buildQueue.RemoveAt(0); if (multithreaded) { buildThread.Interrupt(); } else if (buildSegment != null) { buildSegment.Extrude(); postBuild = true; } } } } if (framesPassed < framesBetweenBuilds) { framesPassed++; } }
void BuildThread() { while (true) { try { Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) { lock (locker) { if (buildSegment != null) { try { buildSegment.Extrude(); postBuild = true; } catch (UnityException ex) { Debug.Log("Segment builder Unity extepion in thread: " + ex.Message); } catch (System.Exception ex) { Debug.Log("Segment builder System extepion in thread: " + ex.Message); } } } } catch (ThreadAbortException) { break; } } }