简体   繁体   中英

How can I update a canvas on the UI without blocking the UI thread?

I am using an open source graphing library, oxyplot, in ac# wpf application. I am running into an issue where the UI thread is blocked when updating the graphs canvas object. Due to the canvas object belonging to the UI thread the update is done on the UI thread.

I have already moved all code I can to run on background threads, but the actual draw of the canvas isn't as easily solved.

The writer of the library tried with this piece of code using a scheduler from the UI thread's synchronization context but still couldn't get around the UI thread blocking when writing to the canvas.

The example above does work pretty much like the plotting library does so I won't give any of the code for that here.

How could I implement this without blocking the UI thread?

EDIT:

Here is a snippet of my code - it shows how i've attempted to implement this. You can see that I create the path data in the first bit of code. When i'm finished I attempt to loop round these on the UI Thread and add them to the canvas. However, once passed into my Add(p) call I get the error that the object belongs to another thread - even though I have past the pa.ToList() into the addToCanvas call...

        .........
            pa.Add(path);
        }

        Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                addToCanvas(pa.ToList());
            }));
    }

    public void addToCanvas(List<Path> path)
    {
        foreach (Path p in path)
        {
            Add(p);
        }
    }

我认为您必须在这里使用一些技巧,例如双重缓冲-创建一个缓冲区(字节,位图等)并将其绘制到单独的线程中,然后将绘制的内容放入控件中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM