画布未由后台线程实时更新

发布于 2024-12-03 08:23:51 字数 1265 浏览 2 评论 0原文

在.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2024-12-10 08:23:51

Dispatcher.BeginInvoke 是异步的,您是否尝试过其同步对应的 Invoke

这至少应该强制所有排队的委托都以正确的顺序执行,但不确定时间。

除此之外,您可能还想尝试更高的 DispatcherPriority。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文