Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello i want to use in my project a while loop for specific time (15 seconds for example).
like this:



What I have tried:

//DateTime timeToStartUpAgain = new DateTime();
//while(DateTime.Now < timeToStartUpAgain) {
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
Updated 18-May-16 1:47am
Sergey Alexandrovich Kryukov 17-May-16 14:01pm    
Member 11436383 17-May-16 14:11pm    
in my project i have to calculate the heart rate of a patient and to send them for client(mobile app)i will wait 1 min for the calculation so i calculate for 15 sec and then *4 better.
int countt = DateTime.Now.Second;

while (DateTime.Now.Second != countt - 1)
this one for 59 sec
Sergey Alexandrovich Kryukov 17-May-16 14:39pm    
I see, thank you. I wrote and answer for you, but, to be serious, the real solution depends on how you communicate with the hardware. Normally, all hardware should be in separate thread(s). Do you have such thing? If not, what you do is no different from a usual watch or stop timer, in your application or not.
Absolutely no need to use DateTime.
Kornfeld Eliyahu Peter 17-May-16 14:11pm    
Simple counting loop (for or while) can not be used for timed execution...Try using Timer...

I cannot imagine why it can be useful; it sounds like a abuse to me.

Anyway, the best way to do it is using System.Diagnostics.Stopwatch. You start it before loop and check in loop and break it when it's time to break: Stopwatch Class (System.Diagnostics)[^].

Of course, the accuracy cannot be better than the time of a single iteration. You can also check up time several times in different part of the code of the iteration. Ultimately, if the code is highly structured, you can have checkup on different places and different levels and, instead of break, throw a custom exception, which you can catch outside the loop. If the time spend inside a single iteration is considerable, it can give you much better precision, but you may loose some side effect you would need to complete in each iteration. In other words, you need to design it.

Another approach with better accuracy could be having a separate thread which you could abort by an outside timer, but this approach is risky unless you really deeply understand the technology and are extremely careful.

Share this answer
try this.

using System;

namespace ConsoleApplication1

    class Program

        static bool flag = true;
        static System.Timers.Timer timer = new System.Timers.Timer();
        static void Main(string[] args)
            int seconds = 3; // seconds to run the loop
            timer.Interval = seconds * 1000;
            timer.Elapsed += timer_Elapsed;

            while (flag)
                // your code.



        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            flag = false;

Share this answer
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900