Skip to content

Calculate elapsed seconds

George George

Asked Apr 12, 2008, 6:49 AM EDT1 answer
Share
LinkedInX

Hello everyone,


What is the most convenient way to check whether elapsed time from some time point to another time point is larger than 2 seconds?

Currently, I am using TimeSpan, and I am using stupid method to check hour/minute/second one by one to decide. Are there any quick way to check?


thanks in advance,
George

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    static int counter = 0;
    static bool b = false;
    myTimer.Tick+=new EventHandler(myTimer_Tick);
    myTimer.Interval = 1000;
    myTimer.Start();
    while (b == false)
     
    {
    C#
         // implement your code that corresond to the time not elapsed yet situation
         if(b==true)
         {
           // implement your code that corresond to the time already  elapsed  situation
           break;
    
         }
     }


    //implement this event handler method like this
    static void myTimer_Tick(Object sender, EventArgs e)
    C#
            {
                // Stop the timer
                myTimer.Stop();
                /* Here we chose 2 seconds or a little more to be in timeout situation */
                if (counter < 2)
                {
                    // Enable the counter
                    myTimer.Enabled = true;
                    // Increment the counter
                    counter++;
                }
                // The condition related to witch the Spalsh form will be disposed
                if (counter == 2) b = true;
    
            }
    0