Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Having windows phone OS 7.1 and it is for better understanding the game pipeline.

Game.IsFixedTimeStep = true and TargetElapsedTime is 60

The idea was to start a new thread and use Thread.Sleep()

protected override void Update(GameTime gameTime)
{
    Sleep();

   base.Update(gameTime);
}

bool m_bSleepRunning;
void Sleep()
{
   m_bSleepRunning = true;
   new System.Threading.Thread(
     () => 
               { 
                    Thread.Sleep(2000);
                    m_bSleepRunning =false;
               ;}
     ).Start();
   while(m_bSleepRunning)
   {
      //empty cycle
   }
}

While debug there is a pause for every update, but the property gameTime.ElapsedGameTime.TotalMilliseconds is not updated.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

By calling Thread.Sleep() inside of a new thread the main thread will not sleep and your Update method is not blocked. To achieve the question you asked give this a shot:

protected override void Update(GameTime gameTime) 
{ 
    System.Threading.Thread.Sleep(2000);
    base.Update(gameTime); 
}

If, on the other hand, your objective is to only update something every so often, try something like this:

float elapsedTime = 0f;
protected override void Update(GameTime gameTime) 
{ 
    elapsedTime += gameTime.ElapsedGameTime.TotalMilliseconds;

    if(elapsedTime >= 2000)
    {
        elapsedTime = 0;
        // run the code you want to happen every so often here
    }

    base.Update(gameTime); 
}

The first code block will bring your entire game to a grinding halt for two seconds, this may be what you are trying to do. The second code block will keep your game running, but allow you to execute some code only every two seconds.

share|improve this answer
    
No, no. Your answer is for simple cases. I want simulate pause for fake calcualtion or exhaustive task and get TotalMilleseconds. Your first block just locking UI Thread and of caurse it does not work for me. –  Artru Aug 8 '12 at 16:20
    
@Artu total milliseconds will be 2000 +- 16.6. You're explicitly specifying that in your example. This answer correctly answers your question. Modify your question if this isn't what you're looking for. –  David Lively Aug 8 '12 at 16:50
    
@Artru I don't understand what you're trying to do. My first code block does what it looks like your code is trying to do. My second code block allows you to perform a task every so often without blocking the UI. If you are performing an exhaustive task in the UI thread, it will block. Based on your use of threads above and your updated question, it seems that you are trying to synchronize the UI thread with a worker thread, in which case you should do some research and find out how to best do that in your situation. Something like DavidLively's solution should be a good start. –  Nate Aug 8 '12 at 20:50
    
I edited the question, sorry for misleading –  Artru Aug 9 '12 at 6:08
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.