void ComputeThread()
        {
            //Console.WriteLine("Start COMPUTE");

            var canceltoken = canceltokensource.Token;
            var computed    = new BlockingCollection <StarGrid>();

            this.computed = computed;

            while (WaitHandle.WaitAny(new WaitHandle[] { canceltoken.WaitHandle, ewh }) == 1)
            {
                while (!canceltoken.IsCancellationRequested)
                {
                    float    mindist = float.MaxValue;
                    float    maxdist = 0;
                    StarGrid selmin  = null;
                    StarGrid selmax  = null;

                    for (int i = grids.Count - 1; i >= 0; i--)              // go backwards thru the list, so the ones painted last gets considered first
                    {
                        StarGrid gcheck = grids[i];

                        //if (gcheck.Id == 2200)
                        //System.Diagnostics.Debug.WriteLine("Cell 2200 ");

                        if (gcheck.Id >= 0 && !gcheck.Working)                                      // if not a special grid
                        {
                            float dist = gcheck.DistanceFrom(curx, curz);
                            if (Math.Abs(dist - gcheck.CalculatedDistance) > MinRecalcDistance) // if its too small a change, ignore.. histerisis
                            {
                                int percentage = GetPercentage(dist);

                                if (percentage > gcheck.Percentage)                // if increase, it has priority..
                                {
                                    if (dist < mindist)                            // if best.. pick
                                    {
                                        mindist = dist;
                                        selmin  = gcheck;
                                        //Console.WriteLine("Select {0} incr perc {1} to {2} dist {3,8:0.0}", gcheck.Id, gcheck.Percentage, percentage, dist);
                                    }
                                }
                                else if (selmin == null && percentage < gcheck.Percentage)   // if not selected a min one, pick the further one to decrease
                                {
                                    if (dist > maxdist)
                                    {
                                        maxdist = dist;
                                        selmax  = gcheck;
                                        //Console.WriteLine("Select {0} decr perc {1} to {2} dist {3,8:0.0}", gcheck.Id, gcheck.Percentage, percentage, dist);
                                    }
                                }
                            }
                        }
                    }

                    if (selmin == null)
                    {
                        selmin = selmax;
                    }

                    if (selmin != null)
                    {
                        //System.Diagnostics.Debug.WriteLine("Repaint Grid -- " + selmin.Id + " dist from " + curx + "," + curz + " is " + selmin.DistanceFrom(curz,curz));
                        selmin.Working = true;                                          // stops another go by this thread, only cleared by UI when it has displayed
                        int   prevpercent = selmin.Percentage;
                        float prevdist    = selmin.CalculatedDistance;

                        selmin.CalculatedDistance = selmin.DistanceFrom(curx, curz);
                        selmin.Percentage         = GetPercentage(selmin.CalculatedDistance);
                        selmin.FillFromDB();

                        Thread.MemoryBarrier();                                         // finish above before trying to trigger another go..

                        Debug.Assert(Math.Abs(prevdist - selmin.CalculatedDistance) > MinRecalcDistance);

                        Debug.Assert(!computed.Contains(selmin));

                        //Tools.LogToFile(String.Format("Grid repaint {0} {1}%->{2}% dist {3,8:0.0}->{4,8:0.0} s{5}", selmin.Id, prevpercent, selmin.Percentage, prevdist, selmin.CalculatedDistance, selmin.CountJustMade));

                        computed.Add(selmin);
                    }
                    else
                    {
                        break;              // nothing to do, wait for kick
                    }
                }
            }

            Trace.WriteLine($"{Environment.TickCount} compute exit");
        }
Beispiel #2
0
        void ComputeThread()
        {
            //Console.WriteLine("Start COMPUTE");

            while (true)
            {
                ewh.WaitOne();

                while (true)
                {
                    if (computeExit)
                    {
                        return;
                    }

                    float    mindist = float.MaxValue;
                    float    maxdist = 0;
                    StarGrid selmin  = null;
                    StarGrid selmax  = null;

                    for (int i = grids.Count - 1; i >= 0; i--)              // go backwards thru the list, so the ones painted last gets considered first
                    {
                        StarGrid gcheck = grids[i];

                        if (gcheck.Id >= 0 && !gcheck.Working)                                      // if not a special grid
                        {
                            float dist = gcheck.DistanceFrom(curx, curz);

                            if (Math.Abs(dist - gcheck.CalculatedDistance) > MinRecalcDistance) // if its too small a change, ignore.. histerisis
                            {
                                int percentage = GetPercentage(dist);

                                if (percentage > gcheck.Percentage)                // if increase, it has priority..
                                {
                                    if (dist < mindist)                            // if best.. pick
                                    {
                                        mindist = dist;
                                        selmin  = gcheck;
                                        //Console.WriteLine("Select {0} incr perc {1} to {2} dist {3,8:0.0}", gcheck.Id, gcheck.Percentage, percentage, dist);
                                    }
                                }
                                else if (selmin == null && percentage < gcheck.Percentage)   // if not selected a min one, pick the further one to decrease
                                {
                                    if (dist > maxdist)
                                    {
                                        maxdist = dist;
                                        selmax  = gcheck;
                                        //Console.WriteLine("Select {0} decr perc {1} to {2} dist {3,8:0.0}", gcheck.Id, gcheck.Percentage, percentage, dist);
                                    }
                                }
                            }
                        }
                    }

                    if (selmin == null)
                    {
                        selmin = selmax;
                    }

                    if (selmin != null)
                    {
                        selmin.Working = true;                                          // stops another go by this thread, only cleared by UI when it has displayed
                        int   prevpercent = selmin.Percentage;
                        float prevdist    = selmin.CalculatedDistance;

                        selmin.CalculatedDistance = selmin.DistanceFrom(curx, curz);
                        selmin.Percentage         = GetPercentage(selmin.CalculatedDistance);
                        selmin.FillFromDB();

                        Thread.MemoryBarrier();                                         // finish above before trying to trigger another go..

                        Debug.Assert(Math.Abs(prevdist - selmin.CalculatedDistance) > MinRecalcDistance);

                        Debug.Assert(!computed.Contains(selmin));

                        //Tools.LogToFile(String.Format("Grid repaint {0} {1}%->{2}% dist {3,8:0.0}->{4,8:0.0} s{5}", selmin.Id, prevpercent, selmin.Percentage, prevdist, selmin.CalculatedDistance, selmin.CountJustMade));

                        computed.Add(selmin);
                    }
                    else
                    {
                        break;              // nothing to do, wait for kick
                    }
                }
            }
        }