Alright... so I found numerous examples on the proper way to suspend and pause threads but none really relate to my problem.

I have a thread which runs in the background of my game which does all the simulation of moving players, calculating spaces and ect... I have a pause button for the user if they need to take a break, but I can't find a way to pause the thread nicely without bugs.

run()  { 
while(true)
{     
function1();        
function2();       
function3();      
function4();        
function5();  
} 
}

To try and pause the thread I inserted a while loop which polls a bit whether to continue or not inbetween each function. This doesn't really work good because 1) I have a lot of functions (more than listed in this example) 2) It doesn't work well with thread.sleep(x)'s.

Lots of people suggest having it poll at the end of the run method before it loops back up, but that doesn't make sense since I need it to freeze on the stop as quickly as possible.

Thanks for taking the time to read this.

have you tried Thread.Suspend(); / Thread.Resume(): ?

It works but its really unsafe. Any proper way of doing so?

seems like the while loop is the way to go. maybe stick some of the function calls in a if(!paused) statement?

That always worked when I wrote flash games. Haven't written a game in C# yet. Although I hear XNA has a good thing going.

seems like the while loop is the way to go. maybe stick some of the function calls in a if(!paused) statement?

That always worked when I wrote flash games. Haven't written a game in C# yet. Although I hear XNA has a good thing going.

"To try and pause the thread I inserted a while loop which polls a bit whether to continue or not inbetween each function. This doesn't really work good because 1) I have a lot of functions (more than listed in this example) 2) It doesn't work well with thread.sleep(x)'s." - Original Post

I tried that approach but it does not halt it properly and seems like bad code.

if your functions need to be continuously called in a loop i would do something like this.

backgroundMethod()
{
while(running)
 {
   if(!Paused)
     {
       Function()
      Function2();
      Function ect...
     {
  }
}

if your functions need to be continuously called in a loop i would do something like this.

backgroundMethod()
{
while(running)
 {
   if(!Paused)
     {
       Function()
      Function2();
      Function ect...
     {
  }
}

That doesnt pause the game, I need it to pause while it is in the functions.

Then I am afraid you might have to rethink your game logic. pausing a huge set of tasks can be a huge task in it's self. And the entire game logic should be written around the idea that it needs to check for a paused flag.

Without any more information its hard to say. I wish I could be more help. Sry.

Then I am afraid you might have to rethink your game logic. pausing a huge set of tasks can be a huge task in it's self. And the entire game logic should be written around the idea that it needs to check for a paused flag.

Without any more information its hard to say. I wish I could be more help. Sry.

I dont know why most people dont understand haha.

Say you're playing monopoly online. There is a thread in the background which:
1. Chooses the next player to take their turn
2. Rolls the dice for that player
3. Moves the player depending on how much they rolled
4. Depending on where they land, tell them what they can do
And repeat.

Those 4 steps are the steps in my game I use them as functions.

run() {
while(true){
chooseNextPlayer();
rollDice();
movePlayer()
executeSpace()
}
}

If the user pauses after they roll, the game should pause, not that if statement you recommended, that would just skip the whole function.

Would something like this be sufficient?

run() {
while(true){
chooseNextPlayer();
while(pause){//do nothing}
rollDice();
while(pause){//do nothing}
movePlayer()
while(pause){//do nothing}
executeSpace()
while(pause){//do nothing}
}
}

Although that looks like it would work, if you were going to do it like that, you might want to sleep that thread for 1 to 3 ms in those empty while loop to take some CPU time priority off that empty loop.

I don't think that would be the best way to write a monopoly game as that would be best a event driven game, not a loop driven.

but doing so in a basic background loop is not a bad way to go about it, There are several ways to do this. one, the if statement system I was talking about before would work fine. but adding an int that would keep track of which method the loop is on. then using a switch to call the correct method, then incrementing the current fucntion int.

backgroundMethod()
{
 int stepCount= 0;
while(running)
 {
   if(!Paused)
     {
                        switch (stepCount)
                        {
                            case 0:
                                Function1();
                                 stepCount++;
                                break;
                           case 1:
                                Function2();
                                 stepCount++;
                                break;
                            default:
                                stepCount = 0; //if were out of functions start over
                                break;
                        }
     {
  }
}

Lots of ways to go.

or instead of those while(paused)[//do nothing} statements, replace them with if(paused){monitor.wait}; That would block the background thread until the main thread called monitor.pulse which would resume it.

Of course all these ways would only pause the game after each of those possibly very complicated game methods returned.

Lots of possiblilites

You could set up a ManualResetEvent to determine pause state.
Without going into the nitty gritty (i'll let you do the reading :p), the ManualResetEvent has two states; signalled and unsignalled. If it is unsignalled, any time a thread calls WaitOne() on the event it will pause until the event is set to signalled. The ManualResetEvent is usually used as a signal to tell a thread it is safe to continue. Its intended use is to ensure one thread has finished before another continues, but you could apply it to your pause problem.
In your case, signalled will be the default state and unsignalled will be paused. You can place event.WaitOne() calls throughout your thread methods. Then when the user pauses you call event.Reset() to unsignal the event, the next event.WaitOne line the background thread hits will pause execution until the event is set to signalled again.
Because event.WaitOne() is a single statement you dont need to put code into blocks, which avoids the mess you can get into with an if(!paused) {} style. Just place a event.WaitOne() call every few lines and the next time the code hits one after the reset is called the thread will hold. It will be much closer to instant pause and wont require you to skip any methods or have a lot of unnecesary loops.

Example:

run()
{
event.WaitOne();
Function1();

event.WaitOne();
Function2();

//etc
}

Function1() //function with ManualResetEvent pausing
{
    //do something
    event.WaitOne();
    //do something else
    event.WaitOne();
}

Function2() 
{
    //do something
    event.WaitOne();
    //do something else
    event.WaitOne();
}
Comments
Great solution. wish i though of it.

Do you have any links about event driven games?

Also, thanks for the moniter.pulse/wait, works perfect.

Message/Event driven game structures are a great way to write games, Unfortunately I don't have any good links. There are quite a few books on the subject although I'm unsure how many of them are leaned toward C#.

This article has been dead for over six months. Start a new discussion instead.