❔ How to set a time interval in a timer ?
This is the class:
And this is the Main class that calls the class
I have my program above to check after an interval time, if a process have running longer than the allowed duration or not, and if it is the program will kill the process. The problem I got now is the time take pretty long than I expected. This is the first time I use the timer so Im sure how to adjust the time with it.
27 Replies
freq
is what?
Like what are you actually entering?
Just 1?it take 3 argument is ProcessName, MaximumLifeTime, Frequency. After an interval time Frequency, the code will check if the Process running time has exceed its MaximumLifeTime and if it does then the code will kill the process
for example program.exe notepad 2 3. It means every 3 minutes the code will check if notepad has run more than 2 minutes or not and if it does it will be killed
but when I run my code, it seems take longer than the input time
store the Frequency and MaxLifeTime like you have but also store the TimeElapsed that you can compare against the MaxLifeTime before killing the process
TimeElasped += Frequency, then you can check to see if TimeElapsed >= MaxLifeTime
I should add this into my main ?
something like this
although you are already doing that in KillProcess() so I may be confused on what you are trying to do here
oh I see you want to check the process run time and not the run time that has passed since receiving the arguments my bad
ye, I was about to explain
right so
var timer = new System.Timers.Timer(freq * 60000);
means it will run timer.Elapsed += (s, e) =>
every 3 minutes if you are passing in 3 as the freq
is that what you are expecting?yes
but it seems take longer than the expecting time
how have you come to that conclusion?
how much longer?
i've tried with 1 minute
and it seems take around 2 minute, give or take few seconds
I also have the code to write down the record
that's odd, testing your code it runs how it should
hmmm
maybe because of my hardware ?
the only thing I see which is extra odd is you are writing one minute has passed in your elapsed event when that may not be true
elapsed will run (freq * 60000), it doesn't necessarily run every minute
just to check every minute passed when I run the program
yes, elapsed however will run every x milliseconds that you define in your constructor, if you want to check every minute then you need to define one minute or use another Timer
so how can i define it as minute ?
if freq = 3 then your elapsed will run every 3 minutes and your WriteLine will print every 3 minutes
ye, its what i'm expected
alright, I just thought it was odd you write "One minute passed" when that may not be the case if freq does not equal 1 is all
so i guess the long time i received is because of my hardware
I don't know
if so, there's nothing need to change it this situation i guess
so can I ask an addition question here ?
this works perfectly on my end without any issue
what is the Task.Delay doing ?
thats just part of my testbench to delay the application from exiting
just ignore it
i removed the irrelevant code
tks for the reference tho, I'll check it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.