While playing, I use a thread to Load() and Unload() Texture2D.
I have multiple ContentManager to only Unload() Texture2D I want to unload.
But sometimes, I have this issue on the SpriteBatch.End() :
ObjectDisposedException: Cannot access a disposed object
Object name : texture2D
I don't understand very well the Threads, and I think it's the main problem.
Roughly :
Game.Update(GameTime gameTime)
{
character.Update(gameTime);
//...Things.Update(gameTime)...
CheckThingsToLoadandUnload();
//etc
}
CheckThingsToLoadAndUnload()
{
For each texture to check
{
if(HasPrerequists)
{
if(!thisTexture.isLoaded)
{
ThreadStart ths_LoadInGame = delegate {
LoadInGame(param1, param2, param3); };
new Thread(ths_LoadInGame).Start();
}
}
else
{
if(thisTexture.isLoaded)
{
ThreadStart ths_UnloadInGame = delegate {
UnloadInGame (param1, param2); };
new Thread(ths_UnloadInGame ).Start();
}
}
}
}
I overrided the T Load<T<(string assetname) ContentManager to add a Lock.
I think this issue happens when the Unload() Thread unload a Texture used by the SpriteBatch().
But I really don't know how to prevent this.
Hope you can help.