Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get passed milliseconds from Timer [Solved]

Get passed milliseconds from Timer [Solved]

Scheduled Pinned Locked Moved C#
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xmen Real
    wrote on last edited by
    #1

    I'm developing a countdown timee that will support Pause function and I'm wondering that how can I get the passed milliseconds from Timer. I'm not sure that it is possible but perhaps it might possible through API. I do not want timer calls tick event after each millisecond, what I want is once I set Interval(eg. 5000) then if I call Pause just after 3000 passed then when resume it should start with 3000. It would be easily possible if I can get the Timer passed millisecond... any idea guys or any other way ? edited this helped me

    Environment.TickCount

    thanks all of you guys ;)

    TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

    ----------------------------------------------- 128 bit encrypted signature, crack if you can

    modified on Thursday, April 9, 2009 8:54 AM

    R _ 2 Replies Last reply
    0
    • X Xmen Real

      I'm developing a countdown timee that will support Pause function and I'm wondering that how can I get the passed milliseconds from Timer. I'm not sure that it is possible but perhaps it might possible through API. I do not want timer calls tick event after each millisecond, what I want is once I set Interval(eg. 5000) then if I call Pause just after 3000 passed then when resume it should start with 3000. It would be easily possible if I can get the Timer passed millisecond... any idea guys or any other way ? edited this helped me

      Environment.TickCount

      thanks all of you guys ;)

      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

      ----------------------------------------------- 128 bit encrypted signature, crack if you can

      modified on Thursday, April 9, 2009 8:54 AM

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      You could use DateTime.Now and subtract one from another to give you a timespan. Timers I don't believe allow you to access their internals. DateTime has a resolution of about 15ms I believe.

      Regards, Rob Philpott.

      _ 1 Reply Last reply
      0
      • R Rob Philpott

        You could use DateTime.Now and subtract one from another to give you a timespan. Timers I don't believe allow you to access their internals. DateTime has a resolution of about 15ms I believe.

        Regards, Rob Philpott.

        _ Offline
        _ Offline
        _groo_
        wrote on last edited by
        #3

        DateTime itself has a resolution of 1 tick, which is 100ns. What you probably wanted to say was that Windows.Forms.Timer is not very precise, because it always invokes the handler from a Gui thread, which means it will sometimes have to wait until OS is done doing Gui stuff before it actually calls the handler delegate.

        R 1 Reply Last reply
        0
        • _ _groo_

          DateTime itself has a resolution of 1 tick, which is 100ns. What you probably wanted to say was that Windows.Forms.Timer is not very precise, because it always invokes the handler from a Gui thread, which means it will sometimes have to wait until OS is done doing Gui stuff before it actually calls the handler delegate.

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          Quite. But DateTime.Now will give not give you 100ns resolution. More like 15ms.

          Regards, Rob Philpott.

          _ 1 Reply Last reply
          0
          • X Xmen Real

            I'm developing a countdown timee that will support Pause function and I'm wondering that how can I get the passed milliseconds from Timer. I'm not sure that it is possible but perhaps it might possible through API. I do not want timer calls tick event after each millisecond, what I want is once I set Interval(eg. 5000) then if I call Pause just after 3000 passed then when resume it should start with 3000. It would be easily possible if I can get the Timer passed millisecond... any idea guys or any other way ? edited this helped me

            Environment.TickCount

            thanks all of you guys ;)

            TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

            ----------------------------------------------- 128 bit encrypted signature, crack if you can

            modified on Thursday, April 9, 2009 8:54 AM

            _ Offline
            _ Offline
            _groo_
            wrote on last edited by
            #5

            You can create a new background thread with a manual reset event, waiting for a specified amount of seconds or an external Set.

            // in your background thread
            int msToWait = 5000;

            do {

            // wait for Start event
            manualStartEvent.WaitOne();

            Stopwatch sw = new Stopwatch();
            sw.Start();
            // wait for Pause event, or until time elapses
            bool externalSet = manualPauseEvent.WaitOne(msToWait);
            sw.Stop();

            // check if Paused or elapsed
            int actualMsElapsed = sw.Elapsed.TotalMilliseconds;
            if (externalSet) // this indicates external set, so time did not elapse
            {
            msToWait -= actualMsElapsed;
            if (msToWait < 0)
            msToWait = 0;
            }
            else // this indicates we're done
            {
            msToWait = 0;
            }

            } while (msToWait > 0);

            [EDIT] Code is posted only as an idea, you should take care of starting the thread, handling Stop situations and proper disposing all events.

            modified on Thursday, April 9, 2009 8:39 AM

            X 1 Reply Last reply
            0
            • R Rob Philpott

              Quite. But DateTime.Now will give not give you 100ns resolution. More like 15ms.

              Regards, Rob Philpott.

              _ Offline
              _ Offline
              _groo_
              wrote on last edited by
              #6

              Good point, resolution of the Now property is in ms, and depends on the system. In Vista it seems to have a 1ms resolution, I just tested it:

              // a console app
              static void Main(string[] args)
              {
              StringBuilder sb = new StringBuilder(16384);
              for (int i = 0; i < 10000; i++)
              sb.AppendLine(String.Format("{0:HH:mm:ss.ffff}", DateTime.Now));

              Console.WriteLine(sb.ToString());
              Console.ReadLine();
              }

              System.Diagnostics.Stopwatch on the other hand is much more precise:

              static void Main(string[] args)
              {
              StringBuilder sb = new StringBuilder(16384);
              Stopwatch sw = new Stopwatch(); sw.Start();
              DateTime now = DateTime.Now;
              for (int i = 0; i < 10000; i++)
              sb.AppendLine(String.Format("{0:HH:mm:ss.fffff}", now + sw.Elapsed));

              Console.WriteLine(sb.ToString());
              Console.ReadLine();
              }

              R 1 Reply Last reply
              0
              • _ _groo_

                You can create a new background thread with a manual reset event, waiting for a specified amount of seconds or an external Set.

                // in your background thread
                int msToWait = 5000;

                do {

                // wait for Start event
                manualStartEvent.WaitOne();

                Stopwatch sw = new Stopwatch();
                sw.Start();
                // wait for Pause event, or until time elapses
                bool externalSet = manualPauseEvent.WaitOne(msToWait);
                sw.Stop();

                // check if Paused or elapsed
                int actualMsElapsed = sw.Elapsed.TotalMilliseconds;
                if (externalSet) // this indicates external set, so time did not elapse
                {
                msToWait -= actualMsElapsed;
                if (msToWait < 0)
                msToWait = 0;
                }
                else // this indicates we're done
                {
                msToWait = 0;
                }

                } while (msToWait > 0);

                [EDIT] Code is posted only as an idea, you should take care of starting the thread, handling Stop situations and proper disposing all events.

                modified on Thursday, April 9, 2009 8:39 AM

                X Offline
                X Offline
                Xmen Real
                wrote on last edited by
                #7

                hmmm nice way...thanks ;)

                TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                ----------------------------------------------- 128 bit encrypted signature, crack if you can

                1 Reply Last reply
                0
                • _ _groo_

                  Good point, resolution of the Now property is in ms, and depends on the system. In Vista it seems to have a 1ms resolution, I just tested it:

                  // a console app
                  static void Main(string[] args)
                  {
                  StringBuilder sb = new StringBuilder(16384);
                  for (int i = 0; i < 10000; i++)
                  sb.AppendLine(String.Format("{0:HH:mm:ss.ffff}", DateTime.Now));

                  Console.WriteLine(sb.ToString());
                  Console.ReadLine();
                  }

                  System.Diagnostics.Stopwatch on the other hand is much more precise:

                  static void Main(string[] args)
                  {
                  StringBuilder sb = new StringBuilder(16384);
                  Stopwatch sw = new Stopwatch(); sw.Start();
                  DateTime now = DateTime.Now;
                  for (int i = 0; i < 10000; i++)
                  sb.AppendLine(String.Format("{0:HH:mm:ss.fffff}", now + sw.Elapsed));

                  Console.WriteLine(sb.ToString());
                  Console.ReadLine();
                  }

                  R Offline
                  R Offline
                  Rob Philpott
                  wrote on last edited by
                  #8

                  Hey nice, stopwatch is new to me. We have some performance timers in the system I work on, but they use raw API calls via interop to do their stuff. Just tried your first example - the elapse is usually 15ms but sometimes 16ms, so on XP I guess the resolution is 15.something ms.

                  Regards, Rob Philpott.

                  L 1 Reply Last reply
                  0
                  • R Rob Philpott

                    Hey nice, stopwatch is new to me. We have some performance timers in the system I work on, but they use raw API calls via interop to do their stuff. Just tried your first example - the elapse is usually 15ms but sometimes 16ms, so on XP I guess the resolution is 15.something ms.

                    Regards, Rob Philpott.

                    L Offline
                    L Offline
                    Luc 648011
                    wrote on last edited by
                    #9

                    It all depends on the available hardware; you may want to look at the article "Timer surprises, and how to avoid them"[^] which predates .NET 2.0 and hence does not mention StopWatch (which is based on PerformanceCounters anyway). :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups