0

如果我是正确的,我绝对不应该在使用 Stopwatch.GetTimeStamp 时遇到 stackoverflow 错误,尤其是在启动我的程序之后。

这是我的代码:

if (currentTicks >= lastTicks + interval)
        {
            lastTicks = currentTicks;
            return true;
        }

由 Stopwatch.GetTimeStamp() 放入的 currentTicks。这段代码在一个称为无限的方法中(我用它来控制 FPS)。有人有什么想法吗?

编辑:主表单代码:

    Game game;

    public Form1()
    {
        InitializeComponent();
        game = new Game(Stopwatch.Frequency / 45);
        MainLoop();
    }

    public void MainLoop()
    {
        if (game.DrawStuff(Stopwatch.GetTimestamp()))
        {
            Invalidate();
        }

        MainLoop();
    }`

然后,游戏类:

    public long lastTicks { get; set; }

    public double interval { get; set; }

    public Game(double Interval)
    {
        interval = Interval;
    }

    public bool DrawStuff(long currentTicks)
    {
        if (currentTicks >= lastTicks + interval)
        {
            lastTicks = currentTicks;
            return true;
        }

        else
        {
            return false;
        }
    }

它在“if (currentTicks >= lastTicks + interval)”时停止。我可以看到 currentTicks 的值为 30025317628568。其他所有内容都无法评估。

4

2 回答 2

4

您正在递归调用 MainLoop (又名无限递归),这意味着您正在溢出调用堆栈。GetTimeStamp 在这里是一个红鲱鱼。

从内部删除对 MainLoop 的调用,只使用标准的 while 循环:

while (game.DrawStuff(Stopwatch.GetTimestamp()))
{
    Invalidate();
}
于 2011-02-11T08:04:49.330 回答
3

我猜发布的代码是属性调用的一部分currentTickslastTicks甚至是interval.

所以问题原来是关于为属性使用适当的 Caps。

于 2011-02-10T19:08:53.527 回答