/// <summary> /// Starting a work-thread to perform the calculation /// </summary> /// <returns> /// True if the thread started off successfully. /// </returns> public bool StartCalculation() { // we need to get the face from the reference we track Face theface = GetReferencedFace(); if (theface == null) { return(false); } // An instance for the result exchange m_results = new SharedResults(); // The agent does not need the face nor the reference. // It can work with just the bounding box and a density of the grid. // We also pass the Results as an argument. The thread will be adding calculated results // to that object, while here in the analyzer we will read from it. Both operations are thread-safe; m_threadAgent = new ThreadAgent(theface.GetBoundingBox(), 10, m_results); // now we can ask the agent to start the work thread return(m_threadAgent.Start()); }
/// <summary> /// The main method for calculating results for the face analysis. /// </summary> /// <remarks> /// The calculated values do not mean anything particular. /// They are just to demonstrate how to process a potentially /// time-demanding analysis in a delegated work-thread. /// </remarks> /// <param name="data"> /// The instance of a Result object to which the results /// will be periodically delivered until we either finish /// the process or are asked to stop. /// </param> /// private void Run(Object data) { SharedResults results = data as SharedResults; double uRange = m_bbox.Max.U - m_bbox.Min.U; double vRange = m_bbox.Max.V - m_bbox.Min.V; double uStep = uRange / m_density; double vStep = vRange / m_density; for (int u = 0; u <= m_density; u++) { double uPos = m_bbox.Min.U + (u * uStep); double uVal = (double)(u * (m_density - u)); for (int v = 0; v <= m_density; v++) { double vPos = m_bbox.Min.V + (v * vStep); double vVal = (double)(v * (m_density - v)); UV point = new UV(uPos, vPos); double value = Math.Min(uVal, vVal); // We pretend the calculation of values is far more complicated // while what we really do is taking a nap for a few milliseconds Thread.Sleep(100); // If adding the result is not accepted it means the analysis // have been interrupted and we are supposed to get out ASAP if (!results.AddResult(point, value)) { return; } } } // for }
/// <summary> /// A constructor initializes a bounding box and /// the density of the grid for the values to be calculated at. /// </summary> /// public ThreadAgent(BoundingBoxUV bbox, int density, SharedResults results) { m_bbox = bbox; m_density = density; m_results = results; }