Access to dictionary items created in a child chain

I am currently understanding the threads and hope someone can clearly explain how to resolve the following error.

I have a global Dictionary<string, BitmapImage> dicone that I create in the main thread.

The main thread creates a child thread to populate the dictionary with images.

Then the main thread tries to access the dictionary and throws an exception

The calling thread cannot access this object because a different thread owns it

I have no problems accessing the dictionary itself, for example. dic.ContainsKey("key")It works just fine, it is easy when I try to access the BitmapImagedictionary that an error occurs.

I suppose because the BitmapImages stored in the dictionary are created in the child thread and this causes the problem - any ideas how I can get around this?

I looked through a lot of instructions for using threads and recommendations here, but it seems to be telling the whole world how to access the parent thread from the child thread, while I am trying to do the opposite.

Thank.

+5
source share
1 answer

In general, you can only access user interface elements from the stream they created. This means that the main thread creates objects BitmapImageon its own.

, BitmapImage ( Freeze()). , (, , , mutex ).

" " .

+8

All Articles