How to make timer keep runing while loop wait for

2019-01-29 11:27发布

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),

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.

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.

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.

thanks in advance,

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);
}

标签: c# timer
1条回答
狗以群分
2楼-- · 2019-01-29 12:19

tl;dr

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

Long Version:

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();
}

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();
}

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.

查看更多
登录 后发表回答