I have a game grid that updates in "steps"(Conway's game of life, though that isn't related to my problem in particular). I'm trying to create a Play button that runs the simulation automatically until the button is pressed again to pause the simulation. At first I had something like this:
public partial class MainWindow : Window
{
bool running = true;
public MainWindow()
{
InitializeComponent();
bool isProgramRunning = true;
while(isProgramRunning)
{
while(running)
ProcessGeneration();
}
}
}
And my button that plays/pauses the simulation has the following click handler:
private void PlayClick_Handler(object sender, RoutedEventArgs e)
{
PlayButton.Content = ((string)PlayButton.Content == "Play") ? "Pause" : "Play";
running = (running) ? false : true;
}
I thought that this would let my program simply keep looping (isProgramRunning never ends) repeatedly checking whether "running" is true or false, and that pressing the button to toggle "running" would allow me to loop/break out of the loop. But just the while(isProgramRunning) part kills the program (it won't even load). Actually every time I try to use a while(), the program stops responding. Is there a way to get around this?