Get passed milliseconds from Timer [Solved]
-
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%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
----------------------------------------------- 128 bit encrypted signature, crack if you can
modified on Thursday, April 9, 2009 8:54 AM
-
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%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
----------------------------------------------- 128 bit encrypted signature, crack if you can
modified on Thursday, April 9, 2009 8:54 AM
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.
-
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.
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.
-
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.
Quite. But DateTime.Now will give not give you 100ns resolution. More like 15ms.
Regards, Rob Philpott.
-
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%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
----------------------------------------------- 128 bit encrypted signature, crack if you can
modified on Thursday, April 9, 2009 8:54 AM
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
-
Quite. But DateTime.Now will give not give you 100ns resolution. More like 15ms.
Regards, Rob Philpott.
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();
} -
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
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%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
----------------------------------------------- 128 bit encrypted signature, crack if you can
-
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();
}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.
-
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.
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). :)