kkennedy1008

I've got a situation where I'm creating complex visuals. Basically, I'm creating Canvases with a bunch of paths using processor intense algorithms. I want to move this functionality off to another thread, but if I do, I won't be able to show the Canvases on screen since they were created in another thread. With this in mind, I was thinking that I'd create the cavas and then serialize it to XAML and then recompose it on the main thread once all of my paths are computed. Is there something else I could try that I'm not thinking of Also, what if recomposing the XAML takes too long, is there a way to make my UI responsive during that part

Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

Douglas Stockwell

Do you need to use Canvas and Path specifically If you are building something which does not require interactive UIELements you should be able to build up GeometryDrawings on a background thread and "Freeze" (see System.Windows.Freezable) them before handoff to the UI thread.





Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

Drew Marsh

Or even better, since performance is key, may be to use StreamGeometry which you can also build on a background thread and then "freeze" and add to the scene on the main thread.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

kkennedy1008

This is exactly what I was looking for. Just to make sure I understand, I can create anything that doesn't inherit from UIElement in a separate thread



Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

Anon User

Not UIElement... DispatcherObject. DispatcherObjects are tied to the thread on which they are initially created.

In WPF something is usually either a DependencyObject, which subclasses DispatcherObject, or a Freezable.

HTH,
Drew




Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

kkennedy1008

But Freezables ARE DependencyObjects.



Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

Drew Marsh

Duh, sorry, I explained that completely wrong... that's what I get for trying to be quick.

You're right, Freezables are DependencyObjects. They're just a special case DO which, when frozen, take on a quality where they can be shipped across threads and used safely. Technically Freezables aren't thread safe until frozen either. There's a whole section in the SDK that explains Freezables, I suggest checking it out.

So, what I meant to say was that anything that derives from DO, but not by way of Freezable is not thread safe.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

kkennedy1008

So I followed your directions and it's working beautifully. Now, in addition to simply having my background worker thread create a drawing and pass it to the UI thread, I also want to render it to a bitmap. I'm currently using my drawing in a visual which I send to a RenderTargetBitmap. This won't work in the background thread. How can I accomplish this



Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

Drew Marsh

Well you can take this to a whole new level and spawn your own background dispatcher thread and do your work there. Check out this section of the SDK for a sample on how to do that. Look for the section titled "Multiple Windows, Multiple Threads"... don't be fooled though, you don't have to show a Window or anything on the thread to make it work. You still can't share the objects created on this dispatcher thread with the main thread.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

kkennedy1008

Okay, I follow. In this case I'd still be able to pass my Drawing objects, right And then if I created a bitmap, I could pass the RenderTargetBitmap across the threads



Re: Windows Presentation Foundation (WPF) Visual Tree Creation Performance

Drew Marsh

Correct! RenderTargetBitmap is a Freezable, so you should be able to do all your work to render it on that background dispatcher thread, then freeze it and then ultimately add it to your main thread by doing something as simple as setting it as the source of an Image that is part of that logical tree.

HTH,
Drew