画布未由后台线程实时更新
在.NET4.0 WPF中,我想使用后台线程在画布上显示绘图路径。以下 ConsumerJob 在后台正确运行并轮询要绘制的点队列。我使用调度程序来修改主线程上的画布,并且它可以正确渲染。但是,我希望这段代码在调用每个 child.add 时一次显示每个段(如动画)。所发生的情况是整个显示立即渲染,而不是一次渲染一个片段。当孩子一次添加一个时,我应该如何修改代码以呈现显示?
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);
}
);
}
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);
}
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Dispatcher.BeginInvoke
是异步的,您是否尝试过其同步对应的Invoke
?这至少应该强制所有排队的委托都以正确的顺序执行,但不确定时间。
除此之外,您可能还想尝试更高的 DispatcherPriority。
Dispatcher.BeginInvoke
is asynchronous, have you tried its synchronous counterpartInvoke
?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.