krikara
krikara

Reputation: 2445

Pause button for a game c#

I just want to create a simple pause button, and perhaps another button to resume if necessary. I've been looking around and I mostly see Thread.Sleep(), which doesn't work for me because I want the pause to remain paused until the user desires.

Thread.Suspend() doesn't work because that is obsolete now.

I've also seen another solution of creating a second form, however, that doesn't seem to be working for me. Once that second form opens up, the entire program closes.

I'm not sure if this makes a difference, but my program currently uses two threads (main thread running form1 along with another thread). Ideally, everything needs to be paused.

Upvotes: 0

Views: 2386

Answers (2)

OopsUser
OopsUser

Reputation: 4774

The simpliest thing to do is to have some variable/property that the other thread can access.

Bool ShouldPause = false;

The other thread should have in it's game loop something like that :

while(true)
{
  if(!ShouldPause)
  {
    UpdateGame();
  }
  Draw();
}

Then the game will proceed only when the ShouldPause variable is false. I did it several times and it worked perfectly.

You don't want to pause the thread via some "Suspend" like functions because it will prevent from him to draw on the screen and would appear like it's not responding.

Upvotes: 1

Stas
Stas

Reputation: 286

You can use Thread Signaling technique. A good start is to take a look at ManualResetEvent Class

Upvotes: 0

Related Questions