Note that there are some explanatory texts on larger screens.

plurals
  1. POC# problem with closing my small game when the form closing event is fired
    text
    copied!<p>I have for a school assignment created a small game, it is divided into 2 different projects, one project with the form and one with a DLL-file containing the game.</p> <p>The game loop is very simple and looks like this:</p> <pre><code>private void GameLoop(Graphics g) { int lastTick = Kernel32.GetTickCount(); do { if (terminated) break; while ((lastTick + 50) &gt; Kernel32.GetTickCount()) Application.DoEvents(); while (gamePaused) Application.DoEvents(); g.FillRectangle(new SolidBrush(Color.White), 0, 0, 800, 640); DrawWalls(g); MoveMonsters(); DrawMonsters(g); lastTick = Kernel32.GetTickCount(); } while (true); gameRunning = false; } </code></pre> <p>It works as intended and redraws the panel on the form page. On the form page i have a button to quit the current game, this is done simply by the main form calling the game.dll's command TerminateGame() witch set's terminated to true, this also works as intended. Now my problem is when the user clicks on the form close button or presses F4.</p> <p>Then i tried to do the same thing:</p> <pre><code>private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (game.IsRunning) game.TerminateGame(); } </code></pre> <p>But then i keep getting this error: A generic error occurred in GDI+. and it points to this line: g.FillRectangle(new SolidBrush(Color.White), 0, 0, 800, 640);</p> <p>I have no idea why it works when i press the button that just terminates the game and why its not working when the Form is closing, its the same method call.</p> <p>The form closes perfectly if i first press the button and then press F4, it's just when i just press F4 that i keep getting this.</p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload