gameTime is updated by the Game class before Draw() and Update() are called, not when it is read, so it will not change between the time your new thread starts and when it stops.
Your code,
while(m_bSleepRunning)
{
//empty cycle
}
Blocks the UI until the thread exits. This defeats the purpose of using a new thread!
If you just want to be notified when a certain time span has elapsed, and respond to that, @NateBross' answer is correct.
If you really need something running in a separate thread that can't be broken into synchronous steps triggered by Update(), you might try using a callback:
private void Callback(object sender, EventArgs args)
{
/// do whatever you wanted when the thread exits...
_taskIsRunning=false;
_taskCompleted=true;
}
private event EventHandler ThreadCallback;
protected override void Initialize()
{
ThreadCallback += Callback;
}
private bool _startNewTask = false;
private bool _taskIsRunning=false;
private bool _taskCompleted=false;
private void StartTask()
{
if (!_taskIsRunning)
{
_taskIsRunning = true;
new Thread(
() =>
{
/// sleep for two seconds REGARDLESS of update frequency
Thread.Sleep(2000);
if (ThreadCallback != null)
ThreadCallback(this, EventArgs.Empty);
}
).Start();
}
}
protected override void Update(GameTime gameTime)
{
if (_startNewTask)
{
StartTask();
_startNewTask=false;
}
base.Update(gameTime);
}
Note that I just threw this together in Notepad++. I doubt that it compiles.
When you need to start your new task, set _startNewTask to true. Instead of a delegate, you could shorten things up by just placing the code you want to run at the end of the 2000ms in the Thread() body itself.