for loop that works with seconds

hi
how can i make a for loop that increments by 2 seconds
i have two for loop nested one inside another and each one will increments by 2 seconds
for( start and iincrements by 2 seconds)
{
if(..){}
else
{
for(start and increment by 2 seconds)
{....}
}
}




Answer this question

for loop that works with seconds

  • database_mentor

    Check out the Timer class instead it's much more appropriate for this type of thing.

    Also, a loop will not be exactly n seconds, it will be n seconds plus the processing time of the loop. A timer will be n seconds plus or minus about 15 milliseconds (due to OS scheduling) which will be amortized in the long run.



  • Peter R Hawkes

    I'm not too sure I understand you question, but if you are wanting to make something 'sleep' or 'wait' for 2 seconds look into using the following:

    System.Threading.Thread.Sleep(2000); // This will sleep for 2 seconds

    Hope this helps.


  • CodeDjinn


    for (initialization; logical expression ; System.Threading.Thread.Sleep(2000))
    {
    ................
    ................
    for (initialization; logical expression ; System.Threading.Thread.Sleep(2000))
    {
    ................
    ................

    }
    }


    Example Code for Testing:
    this.label1.Text = string.Empty;
    this.label1.Text = System.DateTime.Now.Second.ToString() + ";";
    for (int i = 0; i < 2; System.Threading.Thread.Sleep(2000))
    {
    this.label1.Text += System.DateTime.Now.Second.ToString() + ";";
    i++;
    for (int i = 0; i < 2; System.Threading.Thread.Sleep(2000))
    {
    this.label1.Text += System.DateTime.Now.Second.ToString() + ";";
    i++;
    }
    }


  • Mike Chapman

    thanx
    i want to wait exactly for 10 seconds and between each 2 seconds i have to test for somethings and make conditions
    for( ; t1<12seconds;t1+=2 seconds)
    {
    if(...){....}
    else
    {
    for( ; t2<12seconds;t2+=2 seconds)
    {.....
    con=true;
    break;
    }}
    if(con)break;
    else
    continue;
    }

  • JimBobJoe

    >> for (int i = 0; i < 2; System.Threading.Thread.Sleep(2000))

    That's an abuse of the for() statement.  Swapping the Sleep() and the ++i will have no effect on the operation of the code, but will make it's function much more obvious to the next programmer:



    this.label1.Text = string.Empty;
    this.label1.Text = System.DateTime.Now.Second.ToString() + ";";
    for (int i = 0; i < 2; ++i)
    {
         this.label1.Text += System.DateTime.Now.Second.ToString() + ";";
         System.Threading.Thread.Sleep(2000);
    }

     

     



  • for loop that works with seconds