I currently have a game in which large images larger than 1 MB in size serve as the background. I know exactly when this transition should happen, so I created a loader class to handle these large images in the background, but when I load the images, it still freezes the main thread in which the drawing is executed. Since this code runs on 360, I moved the thread to the 4th hardware thread, but that doesn't seem to help. Below is the class I am using. Any thoughts on why my new content manager, which should be in its stream, interrupts a draw in my main stream, will be appreciated.
namespace FileSystem
{
class TextureContainer
{
public uint uiNumberOfReferences = 0;
public Texture2D texture;
}
static class FileManager
{
static Microsoft.Xna.Framework.Content.ContentManager Content;
static EventWaitHandle wh = new AutoResetEvent(false);
static Dictionary<string, TextureContainer> Texture2DResourceDictionary;
static List<Texture2D> TexturesToDispose;
static List<String> TexturesToLoad;
static int iProcessor = 4;
private static object threadMutex = new object();
private static object Texture2DMutex = new object();
private static object loadingMutex = new object();
private static bool bLoadingTextures = false;
public static bool LoadingTexture
{
get {
lock (loadingMutex)
{
return bLoadingTextures;
}
}
}
public static void Initalize(IServiceProvider serviceProvider, string rootDirectory, int _iProcessor )
{
Content = new Microsoft.Xna.Framework.Content.ContentManager(serviceProvider, rootDirectory);
Texture2DResourceDictionary = new Dictionary<string, TextureContainer>();
TexturesToDispose = new List<Texture2D>();
iProcessor = _iProcessor;
CreateThread();
}
public static void Initalize(IServiceProvider serviceProvider, string rootDirectory)
{
Content = new Microsoft.Xna.Framework.Content.ContentManager(serviceProvider, rootDirectory);
Texture2DResourceDictionary = new Dictionary<string, TextureContainer>();
TexturesToDispose = new List<Texture2D>();
CreateThread();
}
static public void CreateThread()
{
Thread t = new Thread(new ThreadStart(StartThread));
t.Start();
}
static public void StartThread()
{
FileManager.Execute();
}
static private void Execute()
{
#if WINDOWS
#else
Thread.CurrentThread.SetProcessorAffinity(new int[] { iProcessor });
Thread.CurrentThread.IsBackground = true;
#endif
while (true)
{
wh.WaitOne();
lock (threadMutex)
{
lock (loadingMutex)
{
bLoadingTextures = true;
}
bool bContainsKey = false;
for (int con = 0; con < TexturesToLoad.Count; con++)
{
lock (Texture2DMutex)
{
bContainsKey = Texture2DResourceDictionary.ContainsKey(TexturesToLoad[con]);
}
if (bContainsKey)
{
}
else
{
TextureContainer TC = new TextureContainer();
TC.uiNumberOfReferences = 1;
try
{
TC.texture = Content.Load<Texture2D>(TexturesToLoad[con]);
lock (Texture2DMutex)
{
bContainsKey = Texture2DResourceDictionary.ContainsKey(TexturesToLoad[con]);
Texture2DResourceDictionary.Add(TexturesToLoad[con], TC);
}
}
catch (Exception e)
{
}
}
Thread.Sleep(100);
}
}
lock (loadingMutex)
{
bLoadingTextures = false;
}
}
}
static public void LoadTextureList(List<string> _textureList)
{
lock (threadMutex)
{
TexturesToLoad = _textureList;
}
wh.Set();
}
public static Texture2D LoadTexture2D( string _textureName )
{
TextureContainer temp;
lock (Texture2DMutex)
{
bool bContainsKey = false;
lock (Texture2DMutex)
{
bContainsKey = Texture2DResourceDictionary.ContainsKey(_textureName);
if (bContainsKey)
{
temp = Texture2DResourceDictionary[_textureName];
temp.uiNumberOfReferences++;
}
else
{
TextureContainer TC = new TextureContainer();
TC.uiNumberOfReferences = 1;
try
{
TC.texture = Content.Load<Texture2D>(_textureName);
}
catch(Exception e)
{
temp = Texture2DResourceDictionary[_textureName];
temp.uiNumberOfReferences++;
}
Texture2DResourceDictionary.Add(_textureName, TC);
temp = TC;
}
}
}
return temp.texture;
}
public static void RemoveTexture2D(Texture2D texture)
{
foreach (KeyValuePair<string, TextureContainer> pair in Texture2DResourceDictionary)
{
if (pair.Value.texture == texture)
{
if (pair.Value.uiNumberOfReferences <= 1)
{
TexturesToDispose.Add(pair.Value.texture);
Texture2DResourceDictionary.Remove(pair.Key);
break;
}
else
{
pair.Value.uiNumberOfReferences--;
}
}
}
}
public static SpriteFont LoadFont( string _fontName )
{
SpriteFont temp = Content.Load<SpriteFont>( _fontName );
return temp;
}
public static XmlDocument LoadXML( string _fileName )
{
XmlDocument temp = Content.Load<XmlDocument>( _fileName );
return temp;
}
public static SoundEffect LoadSound( string _fileName )
{
SoundEffect temp = Content.Load<SoundEffect>(_fileName);
return temp;
}
}
}