canvas not updated real-time by background thread
In .NET4.0 WPF, I want to display a drawingPath on a canvas using a background thread. The following ConsumerJob is correctly running in the background and polling a queue of points to draw. I use a Dispatcher to modify the canvas on the main thread and it gets correctly rendered. However, I would expect this code to display each segment one at a time as and when each children.add gets invoked(like an animation). What happens is that the whole display gets rendered at once and not one segment at a time.开发者_如何学运维 How should I modify the code to render the display as the children get added one at a time?
public void ConsumerJob()
{
while (true)
{
PointsD pt = (PointsD)queue.Consume();
displayQueue.Enqueue(pt);
pt = Scale(pt);
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(ThreadStart)delegate()
{
Path drawingPath = new Path();
StreamGeometry streamingGeometry = new StreamGeometry();
drawingPath.Stroke = Brushes.Black;
drawingPath.StrokeThickness = 0.5;
using (StreamGeometryContext ctx = streamingGeometry.Open())
{
ctx.BeginFigure(new Point(pt.x0, pt.y0), false, false);
ctx.LineTo(new Point(pt.x1, pt.y1), true, false);
}
streamingGeometry.Freeze();
drawingPath.Data = streamingGeometry;
this.Children.Add(drawingPath);
}
);
}
Dispatcher.BeginInvoke
is asynchronous, have you tried its synchronous counterpart Invoke
?
That should at least enforce that all queued delegates are executed in the right order, not sure about the timing though.
You might also want to try a higher DispatcherPriority in addition to that.
精彩评论