0

Whenever I required a timer in my programs, I always used the command

ping localhost -n %number% > nul

But lately, I have been working on a pretty complex program that requires me to run a timer in the background while other code right after it keeps running. I have never had this problem since whenever I encounter a situation like this, I always simply made my programs start other programs that would keep a track of time for them. But this program is different, this time the timer needs to be run very often and more often than not will only continue for a few seconds. Opening a separate program each time the program needs to keep a track of time would just be too messy. Is there anything that you may know of that could possibly start up a timer like this in the background?

Lurker
  • 1
  • 2
  • Your example shows a method of effecting a delay for a period of time, yet your question asks for a timer. Are you trying to time the execution of parts of the script, or are you trying to delay execution for appearances sake? – T3RR0R Aug 21 '21 at 11:12
  • I was explaining what I usually use during situations in which I need to time something. But the problem here is that, this command stops all of the other commands from running until it is finished! I need a timer that runs in the background while rest of the script runs and doesn't interact with the timer – Lurker Aug 21 '21 at 11:41
  • `ping` is not a timer and does not time anything. A timer times how long something takes to happen. So again are you actually looking for a timer, or are you looking for an animation that displays progress is happening while the command/s get executed. – T3RR0R Aug 21 '21 at 11:51
  • If I heard you correctly I would be looking for an animation that runs while the program gets executed. Let me try to explain I am looking for a timer that runs in the background as the program keeps executing whatever it is doing, program will begin a timer of let's say 5 seconds, and it will run many lines of code every second that execute a certain task, after executing the task, it will check for if the timer has run out. It will keep checking until the timer has run out and then stop. I am not very familiar with complicated batch stuff, I am sorry if it is hard to understand me. – Lurker Aug 21 '21 at 12:02
  • After looking at a few things, I have come across something that people refer to as "shutdown timer" shutdown -s -f -t %number% Is it possible to make a timer like this that instead of shutting down your pc executes a different task possibly? – Lurker Aug 21 '21 at 12:29
  • From the way you've now described the goal, i'm not certain you are looking for an animation. What exactly are you looking to stop, and According to what conditions- `1)` Code has completed AND timer has expired. `2)` Code has completed, timer expiry not relevant. `3)` Timer has expired, Code execution state not Relevant. – T3RR0R Aug 21 '21 at 12:31
  • Multithreading can be used to execute code in parallel. See [here](https://pastebin.com/D86xTVZx) for an example of displaying an animation while intervening lines of code execute. – T3RR0R Aug 21 '21 at 12:33
  • Multithreading sounds like exactly what I am looking for. I want to run an invisible timer that, when it expires, it runs a small line of code. Before expiring however, the user can input whatever they want to input and interact with the program. When it expires, it executes a small line of code and interacts with what the user is seeing, it resets the timer and keeps repeating the process. Is this possible? – Lurker Aug 21 '21 at 12:51
  • I have been using batch very simply through out my years of coding, I have only made simple programs for fun so I definitely have difficulty with things that you may find simple, I do apologize for that but I hope you can help – Lurker Aug 21 '21 at 12:51
  • Multithreading is what you are looking for [and here is an example](https://stackoverflow.com/a/43687060/12343998) of triggering an action on keypress while the script can still continue. It shouldn't be that hard for you to add in a method of tracking [elapsed time](https://stackoverflow.com/a/68454079/12343998) as a secondary trigger. – T3RR0R Aug 21 '21 at 13:06

1 Answers1

-1

I made a timer some time ago and it was made from parts from other stack overflow projects made by differite persons. It is working great on win XP cuz it was made on it and tested there.

So here's the code:

@echo off

set /a c=0

:C

set /a c+=1

ping 1.0 -n 2 -w 1 >nul

if %c% == 5 (do what you want here)

cls

goto C

pause

Forex
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 30 '23 at 16:57