{"id":213,"date":"2008-04-01T19:42:40","date_gmt":"2008-04-01T18:42:40","guid":{"rendered":"http:\/\/blog.m-ri.de\/index.php\/2008\/04\/01\/arithmetik-mit-gettickcount\/"},"modified":"2008-03-28T15:46:47","modified_gmt":"2008-03-28T14:46:47","slug":"arithmetik-mit-gettickcount","status":"publish","type":"post","link":"http:\/\/blog.m-ri.de\/index.php\/2008\/04\/01\/arithmetik-mit-gettickcount\/","title":{"rendered":"Arithmetik mit GetTickCount"},"content":{"rendered":"<p>Immer wieder sehe ich Code um GetTickCount oder Aussagen, die sich um den \u00dcberlauf von GetTickCount drehen, der alle 49,7 Tage entsteht. Muss man also irgendwelche Vorkehrungen treffen, wenn man Zeitdifferenzen errechnen will?<\/p>\n<p>Eigentlich muss man sich gar nicht darum k\u00fcmmern, sofern man in 49,7 Tagen eben mindestens einmal diese Differenz errechnet und das damit daf\u00fcr gesorgt ist, dass diese Differenz nicht gr\u00f6\u00dfer sein kann als eben besagt 49,7 Tage.<\/p>\n<pre line=\"1\" lang=\"cpp\">DWORD dwStart=::GetTickCount(); \r\nwhile (::GetTickCount()-dwStart < dwTimeout) \r\n\u00a0\u00a0 DoSomething();<\/pre>\n<p>Die Arithmetik \u00fcber unsigned Integer macht es an dieser Stelle m\u00f6glich. Dieser Code funktioniert, solange eben DoSomething nicht l\u00e4nger als 49,7 Tage dauert. Das ist die einzige Bedingung!<\/p>\n<p>Leider schweigt sich die normale Windows SDK Doku zu GetTickCount dar\u00fcber aus.<br \/>\n<a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms724408.aspx\">http:\/\/msdn2.microsoft.com\/en-us\/library\/ms724408.aspx<\/a><\/p>\n<p><strong>Eigentlich problematisch an dieser Stelle, ist es nicht Differenzen zu bilden sondern Vergleiche durchzuf\u00fchren :!: <\/strong><\/p>\n<p>Einen interessanten Zusatz dazu findet sich jedoch in der Windows CE Doku. Warum dieser Zusatz nicht auch in der normalen SDK Doku steht ist eigent\u00fcmlich. Im Allgemeinen empfinde ich die CE-Doku in der MSDN immer eher als l\u00e4stig.<br \/>\nEs ist aber eben wahrscheinlicher, dass dein CE Rechner und dementsprechend Programme die darauf laufen, l\u00e4nger als 49,7 Tage laufen als auf einem Desktop, ohne einen Reboot, alleine schon wegen der Security Updates, so kann man sich diesen Umstand zumindest erkl\u00e4ren.<br \/>\n<a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms885645.aspx\">http:\/\/msdn2.microsoft.com\/en-us\/library\/ms885645.aspx<\/a><\/p>\n<blockquote><p>When using GetTickCount, subtraction is safe but comparisons such as<\/p><\/blockquote>\n<pre line=\"1\" lang=\"cpp\">if (GetTickCount() > MyTickCount)<\/pre>\n<blockquote><p>are not. You can use the GetTickCount function to time the duration of an activity as shown in the example below, but using GetTickCount for any other operation will cause issues.<\/p><\/blockquote>\n<pre line=\"1\" lang=\"cpp\">wOldTime = GetTickCount(); \r\nDoSomething(); \r\ndwTimeElapsed = GetTickCount() \u2013 dwOldTime;<\/pre>\n<p>Gleiches findet sich in der Windows Mobile 6 Doku noch etwas besser<br \/>\n<a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa915056.aspx\">http:\/\/msdn2.microsoft.com\/en-us\/library\/aa915056.aspx<\/a><\/p>\n<blockquote><p>When using GetTickCount, subtraction is safe, even if the rollover occurred, and subtraction always yields the correct difference and the number of clock ticks passed between two tick values. Comparing tick values directly does not always yield the correct results; only compare the differences. Be sure that your code can service the difference before the second rollover, that is, before another 49.7 days pass. Comparisons such as the following are not safe:<\/p><\/blockquote>\n<pre line=\"1\" lang=\"cpp\">#define DELTA_TICKS sample_tick_value \r\n\/\/ initialized somewhere in the code \r\nDWORD dwStartTick = GetTickCount(); \r\nDWORD dwEndTick =\u00a0\u00a0 GetTickCount() + DELTA_TICKS;     \r\n\r\n\/\/ The following function fails on a rollover. \r\nBOOL no_compare_tick_difference() \r\n{ \r\n\u00a0 if ( GetTickCount() > dwEndTick ) \r\n\u00a0\u00a0\u00a0 return ( TRUE); \r\n\u00a0 return (FALSE); \r\n}<\/pre>\n<blockquote><p>The following code shows how to properly use GetTickCount by comparing tick differences. This code handles the rollover situation.<\/p><\/blockquote>\n<pre line=\"1\" lang=\"cpp\">BOOL compare_tick_difference() \r\n{ \r\n\u00a0 if ( (GetTickCount() \u2013 dwStartTick) > DELTA_TICKS) \r\n\u00a0\u00a0\u00a0 return ( TRUE); \r\n\u00a0 return (FALSE); \r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Immer wieder sehe ich Code um GetTickCount oder Aussagen, die sich um den \u00dcberlauf von GetTickCount drehen, der alle 49,7 Tage entsteht. Muss man also irgendwelche Vorkehrungen treffen, wenn man Zeitdifferenzen errechnen will? Eigentlich muss man sich gar nicht darum k\u00fcmmern, sofern man in 49,7 Tagen eben mindestens einmal diese Differenz errechnet und das damit &hellip; <a href=\"http:\/\/blog.m-ri.de\/index.php\/2008\/04\/01\/arithmetik-mit-gettickcount\/\" class=\"more-link\"><span class=\"screen-reader-text\">\u201eArithmetik mit GetTickCount\u201c <\/span>weiterlesen<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[30,3,2],"tags":[370,61],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-c","category-programmieren","category-windows-api","tag-c","tag-winapi"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":0,"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.m-ri.de\/index.php\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}