【发布时间】:2012-03-10 22:49:56
【问题描述】:
我正在编写一个返回游戏模式 (int) 和 IP 地址 (string) 的启动画面。这个想法是启动画面运行,接受用户输入,然后使用这些选项运行主游戏。我正在使用一个线程来实现这一点 - 线程从启动屏幕轮询退出请求,然后将值拉出到 program.cs 并在启动时调用 exit()。
主游戏自行运行,没有任何问题,但启用启动画面后,游戏仅运行 1 帧,并且在运行更新方法后似乎被垃圾收集处理。 (如果尝试引用它,则返回 DisposedObjectException 或类似的东西)经过一些调试后,我发现问题出在 exit 命令上。代码如下:
using System;
using System.Threading;
namespace SplashScreen
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
int choice = 0;
string ip = "";
bool runSplash = true;
bool useThreading = true;
bool ignoreThreadResponse = false;
// Debug option, toggle running splash screen
if (runSplash == true)
{
bool splashrunning = true;
using (Splash1 splash = new Splash1())
{
if (useThreading)
{
// Run a thread to poll whether the splash screen has requested an exit every 0.5 seconds
Thread t = new Thread(() =>
{
while (splashrunning)
{
// If splash requests exit pull gameMode choice and IP Address before killing it, then quit this thread
if (splash.requestingExit)
{
choice = splash.choice;
ip = splash.ip;
// The offending piece of code, without this you can simply select an option, force close and second part runs fine
//splash.Exit();
splashrunning = false;
}
Thread.Sleep(500);
}
});
t.Start();
}
splash.Run();
}
}
// If splash screen is not running, assign default values
if(!useThreading || !runSplash || ignoreThreadResponse)
{
choice = 2;
ip = "127.0.0.1";
}
if (choice != 0)
{
// This game is picked up by garbage collection after running Update once
using (Game1 game = new Game1(choice, ip))
{
game.Run();
}
}
}
}
}
当调用 splash.Exit() 时,它会导致 game1 在第一次更新后被收集。如果我禁用线程它工作正常。如果我使用右上角的 X 退出,它工作正常。无论我是否忽略线程响应,如果启用线程并且我调用 splash.Exit(),游戏将无法运行。
我正在寻找的是以下任何一项:
收集第二个游戏的原因。
退出游戏或调用“关闭窗口”(大红色 x)函数的另一种方法。
一种更好的实现方式。
我过去曾使用控制台输入来执行此操作,但我想继续使用图形界面,而不是为用户使用丑陋的命令提示符。
编辑:
原来我快到了。虽然 GSM 可能是正确的做事方式,但对于只想从问题中获取代码并将谨慎抛诸脑后的任何人,您只需添加一个线程即可运行第二个游戏。
我很确定这并不理想,但就我而言,它的调整要少得多。
Thread gt = new Thread(() =>
{
using (Game1 game = new Game1(choice, ip))
{
game.Run();
}
});
gt.Start();
因此,虽然我建议任何人从头开始使用 GSM,但对于其他试图使其运行的人来说,这可能是一个快速的解决方案。
【问题讨论】:
标签: c# multithreading garbage-collection xna