Dicks

Hello, I want to create a new Canvas in another thread. For this I create a new thread and set the apartmentstate to STA.

The new thread then creates the Canvas and stores it in a static generic dictionary (so that another thread might access the Canvas and make modifications to it).

In the thread I use a lock on the dictionary and then add the new Canvas. But the problem is that when I try to retrieve the Canvas from the dictionary in another thread and perform an operation on it I get; "cannot modify because the calling thread is not the owning thread" (or similar).

What am I doing wrong



Re: Windows Presentation Foundation (WPF) Create canvas in new thread

Drew Marsh

This is not supported by WPF. The UI elements must be created on the Dispatcher thread where they will ultimately be displayed. If you want to keep your UI responsive while building a large tree of elements, you need to process the data that represents the UI on a background thread, but marshal the element creation back to the UI thread via Dispatcher::[Begin]Invoke.

The exception to the rule is Freezable subclasses which can be built entirely on the background thread, be "frozen" and then be handed to the UI thread for insertion into the tree.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) Create canvas in new thread

Dicks

Ok, actually I have an asp.net site where on the server side I need to parse some UIElements and cache them. So in asp.net, it is never possible to create UIElements since it's MTA and not STA (Now I just start a new thread with STA and do it there, but as before I cant access them from a new thread)



Re: Windows Presentation Foundation (WPF) Create canvas in new thread

Drew Marsh

Well, I assume you just want to generate images in ASP.NET to spit back to the browser In that case why not just generate the image on the Dispatcher thread and save it to a Stream that it was handed from the ASP.NET thread. Then ASP.NET can either pass it a MemoryStream, or even Response.OutputStream to stream directly back.

If this isn't the case, I guess I need more info to understand why you need the UIElement instances on the ASP.NET thread... 'cause that def. won't work.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) Create canvas in new thread

Dicks

I will try to explain my problem somewhat better;

I have a WPF/e app which has a canvas. The canvas contains some polylines which you can select and remove by dragging a square with your mouse. There is a problem with WPF/e where the PointCollection of the polyline is not accessible. This I try to work around by making a Ajax call to the server where the polylines within the selction area will be found and its indexes returned so that the client can remove the correct lines.

For this I load the XAML from file on the server using XamlReader and perform my operations (I do this in a newly started thread which runs as STA, because a webmethod runs in MTA and cant create/load a Canvas object). I want to keep the canvas objects in memory so if I later need them I dont have to load them again, but the problem is I cannot get access to the previously loaded Canvas because it is owned by another thread.

So the actual problem is WPF/e and the pointcollection but I get no response on the WPF/e forums from MSFT about this, otherwise I wouldnt need to do anything on the server. But im just curious if what I ask would be possible.

I hope this explanation makes the problem more clear





Re: Windows Presentation Foundation (WPF) Create canvas in new thread

MadViking

Hi Drew,

could you please post an example of how you would use the Dispatcher thread to do this

I'm struggelig with this exact problem. I temporarily solved the problem by adding AspCompat="true" to the page,

but this caused a problem when using NHibernate 1.2 for some reason.

Thanks,

Inge