I've been having an issue with creating a new thread to update the audio engine while the main thread performs allocations and gc's from the process of creating a new level scene.
It works for the most part and it solved the problem of the music cutting out during level transition but occasionally or perhaps after a certain number of level loads it hangs and I have to close the game down from task manager so no error message pops up unfortunately.
Basically I have it set up like this:
audioThread = new Thread(new ThreadStart(updateAudio));
audioThread.IsBackground = true;
audioThread.Start();
// Allocate new level objects and initialise world
audioThread.Abort();
The audio thread method is just an infinite loop:
private void updateAudio() {
while(true)
Assets.audioEngine.Update();
}
I'm very new to multi-threading and thread safety in c# so any advice at all would be awesome.
Thanks, Marios
EDIT: I switched the threads around and used locks like this:
audioUpdateThread = new Thread(new ThreadStart(setupWorld));
audioUpdateThread.IsBackground = true;
audioUpdateThread.Start();
lockHelp.loadingLevel = true;
updateAudio();
The main thread updates the audio now:
while(true)
{
Assets.audioEngine.Update();
lock (lockHelp)
if (!lockHelp.loadingLevel)
break;
}
audioUpdateThread.Abort();
When the new thread is done loading the level it locks the helper object and sets loadingLevel to false:
lock (lockHelp)
lockHelp.loadingLevel = false;
I've not had any problems now but a playtester did get a hang at level load.