I am trying to create a game and now i want to check if the game is being closed so i will send a message to the server, how do i do it? (XNA)
+2
A:
Try this (found on this site)
protected override void LoadContent()
{
Form f = Form.FromHandle(Window.Handle) as Form;
if (f != null)
{
f.FormClosing += f_FormClosing;
}
base.LoadContent();
}
void f_FormClosing(object sender, FormClosingEventArgs e)
{
// your code here
}
Keeper
2010-06-08 12:49:21
This requires referencing the System.Windows.Forms assembly, and it is not portable to Xbox 360.
Andrew Russell
2010-06-08 13:36:42
You're right, your answer is portable, mine's not.
Keeper
2010-06-08 13:48:09
+8
A:
The correct method is, in your Game
class, override OnExiting
:
protected override void OnExiting(object sender, EventArgs args)
{
// Do stuff here...
base.OnExiting(sender, args);
}
The documentation for this method is here: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.onexiting.aspx
Alternately you could attach an event to Game.Exiting
.
Or you could override EndRun
, although this doesn't seem to fire on Windows if the user exits with Alt+F4.
Andrew Russell
2010-06-08 13:34:59