UnauthorizedAccessException when trying to load content

I am trying to load a png file but everytime an UnauthorizedAccessException is thrown. When I look at the details I see the message “Invalid cross-thread access”. Can anybody explain to me why this happens? I don’t remember making the game multithreaded, does MonoGame do this on its own?

I try to load content in the update function of a GameComponent. Is this somehow not possible?

I am developing for Windows Phone 8 using MonoGame 3.2, I am using Visual Studio 2012.

I fixed it by converting the assets from png to xnb. I thought png files could be loaded?

Have you tried loading in the LoadContent rather than in Update.? I’ve used the Bitmap class without problems but I’ve only ever loaded during the LoadContent call. The Bitmap class requires a “UI” thread so maybe the thread that calls update is a non-UI one?

I didn’t knew the Bitmap class needs a UI thread, could it be that xnb files are loaded because they do not use the Bitmap class?
I haven’t tried loading the content in the LoadContent method because I managed to fix it by converting to xnb.

The way my code is currently structured means I only load sprites when they are required. If I where to use the LoadContent method would I need to load all my assets right from the start? Even tough I might not need every asset?

Thanks for the help :smile:

I’m not sure which “bit” of the Bitmap class needs to be running on a UI thread but you can find a few questions like this c# - Invalid cross-thread access issue - Stack Overflow with the same sort of problem. Yes I preload all assets in the LoadContent even though some of them may not be used. It saves delays in the update!

Thanks for the extra info. I will try to rewrite the code so I load all assets required in the LoadContent method. Are there any additional benefits to this besides delays in the update (which would be minor in my case)?

That would be a question for someone more experienced than myself. The only thing I can think of is that you will have less coupling between the update and draw calls.