如何让计时器保持运行而循环等待另一个时间结果? [英] How to make timer keep runing while loop wait for another time result?

查看:19
本文介绍了如何让计时器保持运行而循环等待另一个时间结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有以下问题,我在计时器上运行这个循环(这只是在大计时器上运行的循环的一小部分),

i have the following issue in my code, i have this loop running on timer (this is just a small part of the loops that running on the big timer),

在那个大计时器内(他每 1 秒打勾)我有 1 个方法需要等待 5 秒然后继续循环代码的其余部分,但我希望它不会卡住代码并且计时器将继续每 1 秒运行一次,不会等待那 5 秒.

inside that big timer (he tick every 1 second) i have 1 method that need to wait 5 second then continue with the the rest of the loop code, but i want that it wont stuck the code and the timer will continue to run every 1sec and wont wait for those 5sec.

我做了什么,我添加了一个新的计时器 (timer_deva),它每 5 秒滴答一次,并在其中执行所有检查,然后计时器停止.

what i did i add a new timer (timer_deva) that tick every 5sec and did all the checks inside it, and then timer stops.

所以我的问题是我需要等待 5 秒来检索一个值来完成我的代码,但我需要我的主计时器将继续同时运行,当他再次获得结果时,他将需要完成他留下的代码.

so my issue is that i need to wait 5sec to retrieve a value to complete my code, but i need that my main timer will keep running simultaneously, and when he get his result for the other time he will need to complete the code he left behind.

提前致谢,

else if (mobID.Equals(Convert.ToInt32(txtDeva)))
{
    //START CHECK WITH TIMER
    timer_deva.Start();
    //Methods inside timer_deva update the winnerNation
    //END CHECK TIMER - GET RESULT
    winner(zoneId, winnerNation, humansKills, orcKills);
}

推荐答案

tl;dr

游戏中不使用传统计时器.游戏有一个非常不同的机制来处理它们的逻辑和经过的时间.

Conventional Timers are not used in games. Games have a very different mechanism for handling their logic and the time that passed.

长版:

我知道这可能无法直接回答您的问题,但它是一种将大量文本压缩到评论中的方法.你的计时器听起来非常复杂和层次分明.根据您的变量名称,我假设您正在编写游戏.游戏通常不使用计时器或以您期望的方式运行.这种不同的类似游戏的行为会对您的计时器问题有很大帮助,甚至可能对您的总体设计有更多帮助.

I know this may not answer your question directly, but it's way to much text to cramp into a comment. Your timers sound very complicated and hierarchical. From your variable names I will assume you are programming a game. Games normally don't work with timers or not in the way you would expect. This different game-like behaviour would help you a lot in your timers problem and may even help you more with your design in general.

游戏通常有一个叫做游戏循环的东西.一般来说,循环中依次调用三个主要函数:

Games normally have something called a game loop. Generally speaking it's three main functions that are called one after the other in a loop:

while(running)
{
    HandleUserInput();
    ChangeWorld();
    Render();
}

您获得用户输入,相应地更改游戏世界并将其绘制到屏幕上.现在,您的计算机速度越快,此循环运行的速度就越快.这对图形有好处(想想 FPS),但对游戏不利.想象一下俄罗斯方块,方块的每一帧都在移动.现在我不想买更快的电脑,那样游戏会变得更难.

You get user input, you change the game world accordingly and you draw it to the screen. Now, the faster your computer is, the faster this loop runs. That's good for the graphics (think FPS), but bad for the game. Imagine Tetris where every frame the blocks move. Now I would not want to buy a faster computer, the game would get more difficult that way.

所以为了保持游戏速度恒定而不受计算机功率的影响,循环会考虑经过的时间:

So to keep the game speed constant independent of the power of the computer, the loop considers the time passed:

while(running)
{
    var timePassedSinceLastLoop = CalculateTimeDelta();

    HandleUserInput();
    ChangeWorld(timePassedSinceLastLoop);
    Render();
}

现在想象一下游戏中某物的冷却时间.玩家按下a",发生了一些很酷的动作,尽管他可能会再次按下a",但在接下来的 5 秒内不会发生任何事情.但游戏仍然运行并完成游戏中可能发生的所有其他事情.这不是传统的计时器.这是一个变量,我们称之为ActionCooldown,一旦玩家触发动作,它就会被设置为5秒.每次世界发生变化时,都会从该数字中减去 timePassed,直到它为零.游戏一直在运行并处理输入和渲染.但是只有当 ActionCooldown 达到零时,再按一次a"才会再次触发该动作.

Now imagine a cooldown for something in game. The player pressed "a", some cool action happened and although he may press "a" again, nothing will happen for the next 5 seconds. But the game still runs and does all the other things that may happen ingame. This is not a conventional timer. It's a variable, lets call it ActionCooldown, and once the player triggers the action, it's set to 5 seconds. Every time the world changes, the timePassed is subtracted from that number until it's zero. All the time, the game is running and handling input and rendering. But only once ActionCooldown hits zero, another press of "a" will trigger that action again.

这篇关于如何让计时器保持运行而循环等待另一个时间结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆