Execute an operation every x seconds for y minutes in c#

后端 未结 9 673
Happy的楠姐
Happy的楠姐 2021-01-02 06:45

I need to run a function every 5 seconds for 10 minutes.

I use a timer to run it for 5 secs, but how do I limit the timer to only 10 mins?

9条回答
  •  遥遥无期
    2021-01-02 07:26

    Divide the Y minutes by the X interval to get how many times it needs to run. After that you just need to count how many times the function has been called.

    In your case, 10 min = 600 seconds / 5 seconds = 120 calls needed. Just have a counter keep track of how many times your function has been called.

提交回复
热议问题