I have a System.Windows.Controls.Canvas on to which I programatically place a System.Windows.Shapes.Polygon:
private Canvas mMainCanvas = new Canvas();
private Polygon mHistogram = new Polygon();
Later on, I update the polygon with a very large number of events (~1,000,000 or so). I have been trying to make this as fast and efficient as possible (a struggle in its self). My latest attempt was to accumulate the values into a PointCollection and periodically re-set the collection of the Polygon (mHistogram):
int i = 10000;
PointCollection mPc = new PointCollection(256);
double y;
Point p;
private void EventProcessor( int bin ) {
if (0 < i--) {
p = mPc[bin];
y = p.Y + 1;
p.X = p.X;
p.Y = y;
mPc[bin] = p;
if (mMainCanvas.Height < p.Y)
mMainCanvas.Height = p.Y;
}
else {
i = 10000;
mHistogram.Points = new PointCollection( mPc ); /* This works if mPc
is a PointCollection.
It does not work if
mPc is a Point[]
}
}
This seems to work OK, albeit still not fast enough. So, I changed the type of mPc from a PointCollection to a simple array of Points (Point[]), hoping this would make access a little faster. However, when I do this, my Polygon (mHistogram) fails to update at all.
This is baffling to me. I create a new PointCollection from an IEnumerable (mPc) which should create a new PointCollection that behaves just like any other PointCollection. Why does it behave differently when created with an Array (Point[])?
Thanks.
The problem you describe seems unlikely and is probably related to another issue with your code. Looking at this simple test, you can verify that the same collections are in fact created:
As for the assignment to the Points property, the Polygon wouldn’t care how the PointsCollection was created.
Both these pieces of code produce the same results:
and:
Keep in mind that the following are different:
The collection contains 0 items, but has space to hold 256 preallocated. The array contains 256 items and will only ever have space to hold 256 items.