0
\$\begingroup\$

When launching a game multiple times in a console, the first time this.Exit() on the game class is called, all following games do not properly run. Instead they exit immediately.

For example, if I have a function that does:

foreach(var i in options) {
  using(var game = new MyGame()) {
    game.Run();
  }
}

and the game class has

protected override void Update(GameTime gameTime)
{
  if(gameTime.TotalGameTime.TotalSeconds > 3)
    this.Exit();
  base.Update(gameTime);
}

then the first instance will execute just fine (for 3 seconds), but the second will exit immediately.

How can I prevent XNA from making the second games die immediately?

\$\endgroup\$
  • \$\begingroup\$ I don't think XNA supports multiple instances of Game; what are you trying to accomplish? Can you do it with multiple processes instead? \$\endgroup\$ – congusbongus Nov 28 '13 at 5:47
2
\$\begingroup\$

There should only be one instance of Game created in an XNA process I believe, so that code won't work. Although you're using a 'using' directive (which will call Game.Dispose when the scope runs out), that will only clean up unmanaged resources; the game object itself, and any managed resources it holds on to won't have been disposed of by the garbage collector by the time the subsequent MyGame is constructed. I suspect it is internally checking that it is the only instance of a Game-derived class and refusing to do any work.

See this question for some more illumination on the problem.

Game is intended to be a high level object, constructed once, that activates a bunch of services for your game. There may be a way to make it clean up after itself, but I suspect you'll find your life much easier if you move your foreach (var i in options) inside the MyGame class, and spawn a secondary game-like class to do the actual per-option work.

Alternatively you could have the options loop spawn a new process for each game, but then you'd have to co-ordinate cross process to pass the option information in, and to establish when the sub-process has finished running.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Dang. They must be using singletons somewhere and remembering the exist. Kinda unfortunate. \$\endgroup\$ – heneryville Dec 4 '13 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.