Stopwatch.GetTimeStamp 导致堆栈溢出错误?
如果我是正确的,那么在使用 Stopwatch.GetTimeStamp 时,我绝对不会遇到 stackoverflow 错误,尤其是在启动程序后。
这是我的代码:
if (currentTicks >= lastTicks + interval)
{
lastTicks = currentTicks;
return true;
}
由 Stopwatch.GetTimeStamp() 放置的 currentTicks 。这段代码位于一个名为“infinitely”的方法中(我用它来控制 FPS)。有人有什么想法吗?
编辑: 主窗体代码:
Game game;
public Form1()
{
InitializeComponent();
game = new Game(Stopwatch.Frequency / 45);
MainLoop();
}
public void MainLoop()
{
if (game.DrawStuff(Stopwatch.GetTimestamp()))
{
Invalidate();
}
MainLoop();
}`
然后,Game 类:
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。其他所有内容都无法评估。
If I'm correct, I should definitely not be getting a stackoverflow error while using Stopwatch.GetTimeStamp, especially only directly after starting my program.
Here's my code:
if (currentTicks >= lastTicks + interval)
{
lastTicks = currentTicks;
return true;
}
currentTicks being placed in by Stopwatch.GetTimeStamp(). This bit of code is in a method called infinitely (I'm using it to control FPS). Anybody have any ideas?
EDIT:
Main form code:
Game game;
public Form1()
{
InitializeComponent();
game = new Game(Stopwatch.Frequency / 45);
MainLoop();
}
public void MainLoop()
{
if (game.DrawStuff(Stopwatch.GetTimestamp()))
{
Invalidate();
}
MainLoop();
}`
Then, the Game class:
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;
}
}
It stops on "if (currentTicks >= lastTicks + interval)". I can see the value of currentTicks is 30025317628568. Everything else cannot be evaluated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在递归地调用 MainLoop(又名 无限递归),这意味着您正在溢出调用堆栈。 GetTimeStamp 在这里是一个转移注意力的事情。
从内部删除对 MainLoop 的调用,只使用标准 while 循环:
You're calling MainLoop recursively (aka infinite recursion), which means that you're overflowing the call stack. GetTimeStamp is a red herring here.
Remove the call to MainLoop from within itself and just use a standard while loop :
我猜测发布的代码是名为
currentTicks
、lastTicks
甚至interval
的属性的 getter 的一部分。因此,问题在于如何为属性使用适当的上限。
I'm guessing the posted code is part of the getter of a property callled
currentTicks
,lastTicks
or eveninterval
.And so the question turns out to be about using proper Caps for properties.