2016-05-16 39 views
0

我本來想後一個問題,怎麼會是下面的情況可能在多線程,同步代碼:不可能的結果,由於屬性更改事件正在緩慢

Debugging screen

,但我要的答案,而寫下這個問題:在計算時,方程式中的股息似乎爲零,但在此之後迅速改變價值(注意:這個錯誤是罕見的,非正常複製,通常當我用箭頭向上時/向下DataGrid)。

相關的代碼如下所示:

 public double? LeftPolyArea => Model.GetArea(LeftPolyName); 
     public double? RightPolyArea => Model.GetArea(RightPolyName); 
     public double? RightPolyNonOverlappingArea =>Model.GetNonOverlappingArea(RightPolyName, LeftPolyName); 
     public double? ResemblenceIndex 
     { 
      get 
      { 
       if (!(LeftPolyOverlappingArea.HasValue && LeftPolyArea.HasValue && RightPolyArea.HasValue)) return null; 
       var index = LeftPolyOverlappingArea.Value/((LeftPolyArea.Value + RightPolyArea.Value)/2) * 100; 
       return Math.Round(index, 1); 
      } 
     } 

和NotifyPropertyChanged事件被觸發一次:

public void Refresh() 
     { 
      OnPropertyChanged(nameof(LeftPolyArea)); 
      OnPropertyChanged(nameof(RightPolyArea)); 
      OnPropertyChanged(nameof(ResemblenceIndex)); 
     } 

我看到了我堅持通常是通知屬性不會發生,但我真的不明白,爲什麼它不能像現在這樣工作。我會理解,如果這兩個觸發事件改變了一些現有的變量,比quicly訪問會產生「舊」值,但那些是獲取屬性,所以控制流程不應該進展,直到計算完成,對吧?這裏有什麼問題?

[編輯]我加入了型號代碼: [EDIT2]幾乎全型號代碼:

public class SpatialMapsModel : ISpatialMapsModel 
{ 
    public int RoundDigits { get; set; } = 1; 

    public IOService InputOutputService { get; } 

    public SpatialMapsModel(IOService ioService) 
    { 
     InputOutputService = ioService; 
    } 

    public bool IsPolygonValid(IList<C2DPoint> polygon) 
    { 
     return polygon?.Count > 2 
    } 

    public enum IntersectionType 
    { 
     Overlapping, 
     NonOverlapping 
    } 

    public List<C2DHoledPolygon> GetIntersectingPolygons(IList<C2DPoint> pointsA, IList<C2DPoint> pointsB, IntersectionType whichPolygons) 
    { 
     var leftPoly = new C2DPolygon(pointsA.ToList(), true); 
     var rightPoly = new C2DPolygon(pointsB.ToList(), true); 
     rightPoly.RandomPerturb(); 
     var someGrid = new CGrid(); 
     var smallPolygons = new List<C2DHoledPolygon>(); 
     switch (whichPolygons) 
     { 
      case IntersectionType.Overlapping: 
       leftPoly.GetOverlaps(rightPoly, smallPolygons, someGrid); 
       break; 
      case IntersectionType.NonOverlapping: 
       leftPoly.GetNonOverlaps(rightPoly, smallPolygons, someGrid); 
       break; 
      default: 
       throw new ArgumentException(nameof(whichPolygons)); 
     } 
     return smallPolygons; 
    } 

    public double? GetOverlappingArea(IList<C2DPoint> pointsA, IList<C2DPoint> pointsB) 
    { 
     var polygons = GetIntersectingPolygons(pointsA, pointsB, IntersectionType.Overlapping); 
     var area = polygons.Sum(p => p.GetArea()); 
     return Math.Round(area, RoundDigits); 
    } 

    public double? GetNonOverlappingArea(IList<C2DPoint> pointsA, IList<C2DPoint> pointsB) 
    { 
     var polygons = GetIntersectingPolygons(pointsA, pointsB, IntersectionType.NonOverlapping); 
     var area = polygons.Sum(p => p.GetArea()); 
     return Math.Round(area, RoundDigits); 
    } 

    private Tuple<double, double, double, double> MinMax(IList<C2DPoint> input) 
    { 
     var minX = double.MaxValue; 
     var minY = double.MaxValue; 
     var maxX = double.MinValue; 
     var maxY = double.MinValue; 
     foreach (var t in input) 
     { 
      if (t.X < minX) 
       minX = t.x; 
      if (t.y < minY) 
       minY = t.y; 
      if (t.X > maxX) 
       maxX = t.x; 
      if (t.y > maxY) 
       maxY = t.y; 
     } 
     return new Tuple<double, double, double, double>(minX, minY, maxX, maxY); 
    } 
    public void SnapToOriginInPlace(IList<C2DPoint> input) 
    { 
     var minXy = MinMax(input); 
     for (var i = 0; i < input.Count; ++i) 
     { 
      input[i] = new C2DPoint(input[i].X - minXy.Item1, input[i].Y - minXy.Item2); 
     } 
     var poly = new C2DPolygon(input.ToList(), true); 
     poly.RandomPerturb(); 
     var pointsCopy = new C2DPointSet(); 
     poly.GetPointsCopy(pointsCopy); 
     for (var i = 0; i < pointsCopy.Count; ++i) 
     { 
      input[i] = pointsCopy[i]; 
     } 
    } 
    public KeyValuePair<string, List<C2DPoint>> GetPolygonFromFile(string fileName){...} 

    public void WritePolygonToFile(IList<C2DPoint> poly, string fileName){...} 

    public double? GetArea(IList<C2DPoint> points) 
    { 
     if (IsPolygonValid(points)) 
     { 
      var poly = new C2DPolygon(points.ToList(), true); 
      var area = poly.GetArea(); 
      return Math.Round(area, RoundDigits); 
     } 
     return null; 
    } 

    public double? GetPerimeter(IList<C2DPoint> points){...} 
} 
+0

您知道所有UI都是單線程的,並且屬性更改事件不在多線程上運行? – TomTom

+0

沒有多線程 –

+0

我們可以看到'Model'的代碼嗎? – weston

回答

0

的問題是從我使用這個庫,GeoLib,當X或Y的值來的一個點恰好等於其他X或Y,當得到重疊時。隨機擾動應該防止這種情況發生,但是它會隨機地(隨機地)......不這樣做,特別是在縮放和轉換多邊形時。 Hovewer仍然沒有回答,爲什麼計算過程中的值會發生變化。

相關問題