473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What is the GetTickCount() equivalent in C#?

EOS
Hi,

As the title indicated, does anyone know the equivalent of GetTickCount()
function in C/C++ for C#?

The function is supposed to return time elapsed since the computer/device is
booted in milisecond.

By te waym thanks for all the previous help. It is really not easy to
convert current C/C++ codes into the new C# as it is more than different
syntax. I am having headache converting a simple few pages of C/C++ into
C#... At times, I would like to have a simple memcpy in C#.
Dec 2 '05 #1
23 73726
Easy. Use the Ticks property of the static DateTime.Now, as in DateTime.Now.Ticks.
Hi,

As the title indicated, does anyone know the equivalent of
GetTickCount() function in C/C++ for C#?

The function is supposed to return time elapsed since the
computer/device is booted in milisecond.

By te waym thanks for all the previous help. It is really not easy to
convert current C/C++ codes into the new C# as it is more than
different syntax. I am having headache converting a simple few pages
of C/C++ into C#... At times, I would like to have a simple memcpy in
C#.

Dec 2 '05 #2
You should still be able to use it using p/invoke.

It is part of the kernel32.dll.

"EOS" <no****@nospam.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

As the title indicated, does anyone know the equivalent of GetTickCount()
function in C/C++ for C#?

The function is supposed to return time elapsed since the computer/device
is booted in milisecond.

By te waym thanks for all the previous help. It is really not easy to
convert current C/C++ codes into the new C# as it is more than different
syntax. I am having headache converting a simple few pages of C/C++ into
C#... At times, I would like to have a simple memcpy in C#.

Dec 2 '05 #3
>As the title indicated, does anyone know the equivalent of GetTickCount()
function in C/C++ for C#?
System.Environment.TickCount

The function is supposed to return time elapsed since the computer/device is
booted in milisecond.


Then the above is no good since it will wrap around after ~50 days.
Consider checking the System Up Time performance counter instead.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 2 '05 #4
Use Environment.TickCount property.
Dec 2 '05 #5
David,

DateTime.Now.Ticks will return the time elapsed since Jan 1st 0001 12:00 AM
in tick units where each unit is 100 nanoseconds. Nothing to do with the
time ellapsed since last time the system has started.

The property to look at is System.Environment.TickCount

I'd suggest reading the help for this property carefully because there might
be some gotchas.
Keep in mind that the C++ methods returns the result in DWORD, which is 32
bit unsigned integer. System.Environment.TickCount is Int32, which is 32 bit
signed integer. That means System.Environment.TickCount will wrap around to
zero sooner than the C++ method - Int32 is half smaller that DWORD in terms
of positive numbers.

--
HTH
Stoitcho Goutsev (100) [C# MVP]

"David Smith" <fl*******@nospam.com> wrote in message
news:2b************************@news.microsoft.com ...
Easy. Use the Ticks property of the static DateTime.Now, as in
DateTime.Now.Ticks.
Hi,

As the title indicated, does anyone know the equivalent of
GetTickCount() function in C/C++ for C#?

The function is supposed to return time elapsed since the
computer/device is booted in milisecond.

By te waym thanks for all the previous help. It is really not easy to
convert current C/C++ codes into the new C# as it is more than
different syntax. I am having headache converting a simple few pages
of C/C++ into C#... At times, I would like to have a simple memcpy in
C#.


Dec 2 '05 #6
EOS
I am confussed here.

Since GetTickCount() return miliseconds in DWROD (32-bits) and will reach
its limit after around 49 days... and started again.

While how could a higher accuracy of 100 nanoseconds start with Jan 1st 0001
12:00AM? Unless it is 64 bit then...

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
David,

DateTime.Now.Ticks will return the time elapsed since Jan 1st 0001 12:00
AM in tick units where each unit is 100 nanoseconds. Nothing to do with
the time ellapsed since last time the system has started.

The property to look at is System.Environment.TickCount

I'd suggest reading the help for this property carefully because there
might be some gotchas.
Keep in mind that the C++ methods returns the result in DWORD, which is 32
bit unsigned integer. System.Environment.TickCount is Int32, which is 32
bit signed integer. That means System.Environment.TickCount will wrap
around to zero sooner than the C++ method - Int32 is half smaller that
DWORD in terms of positive numbers.

--
HTH
Stoitcho Goutsev (100) [C# MVP]

"David Smith" <fl*******@nospam.com> wrote in message
news:2b************************@news.microsoft.com ...
Easy. Use the Ticks property of the static DateTime.Now, as in
DateTime.Now.Ticks.
Hi,

As the title indicated, does anyone know the equivalent of
GetTickCount() function in C/C++ for C#?

The function is supposed to return time elapsed since the
computer/device is booted in milisecond.

By te waym thanks for all the previous help. It is really not easy to
convert current C/C++ codes into the new C# as it is more than
different syntax. I am having headache converting a simple few pages
of C/C++ into C#... At times, I would like to have a simple memcpy in
C#.



Dec 3 '05 #7
EOS
I am sorry that I did not specify it is meant for PDA, compact .net

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
You should still be able to use it using p/invoke.

It is part of the kernel32.dll.

"EOS" <no****@nospam.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

As the title indicated, does anyone know the equivalent of GetTickCount()
function in C/C++ for C#?

The function is supposed to return time elapsed since the computer/device
is booted in milisecond.

By te waym thanks for all the previous help. It is really not easy to
convert current C/C++ codes into the new C# as it is more than different
syntax. I am having headache converting a simple few pages of C/C++ into
C#... At times, I would like to have a simple memcpy in C#.


Dec 3 '05 #8
EOS
Thanks for the straight answer.

I guess 49~50 days is perfectly acceptable since I am developing for PDA,
which usually require re-boot within the duration. Lame but truth.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
As the title indicated, does anyone know the equivalent of GetTickCount()
function in C/C++ for C#?


System.Environment.TickCount

The function is supposed to return time elapsed since the computer/device
is
booted in milisecond.


Then the above is no good since it will wrap around after ~50 days.
Consider checking the System Up Time performance counter instead.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Dec 3 '05 #9
EOS,

System.Environment.TickCount has nothing to do with DateTime and its
100-nanoseconds accuracy. They are implemented differently and they
expilicitly state this in MSDN:

MSDN:
" Remarks
The value of this property is derived from the system timer and is stored as
a 32-bit signed integer. Therefore, the elapsed time will wrap around to
zero if the system is run continuously for 24.9 days.

The resolution of the TickCount property cannot be less than 500
milliseconds.

The TickCount property handles an overflow condition by resetting its value
to zero. The minimum value returned by TickCount is 0.

TickCount is different from the Ticks property, which is the number of
100-nanosecond intervals that have elapsed since 1/1/0001, 12:00am.

Use the DateTime.Now property to obtain the current local date and time on
this computer. "
--

Stoitcho Goutsev (100) [C# MVP]

"EOS" <no****@nospam.org> wrote in message
news:OD****************@TK2MSFTNGP14.phx.gbl...
I am confussed here.

Since GetTickCount() return miliseconds in DWROD (32-bits) and will reach
its limit after around 49 days... and started again.

While how could a higher accuracy of 100 nanoseconds start with Jan 1st
0001 12:00AM? Unless it is 64 bit then...

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
David,

DateTime.Now.Ticks will return the time elapsed since Jan 1st 0001 12:00
AM in tick units where each unit is 100 nanoseconds. Nothing to do with
the time ellapsed since last time the system has started.

The property to look at is System.Environment.TickCount

I'd suggest reading the help for this property carefully because there
might be some gotchas.
Keep in mind that the C++ methods returns the result in DWORD, which is
32 bit unsigned integer. System.Environment.TickCount is Int32, which is
32 bit signed integer. That means System.Environment.TickCount will wrap
around to zero sooner than the C++ method - Int32 is half smaller that
DWORD in terms of positive numbers.

--
HTH
Stoitcho Goutsev (100) [C# MVP]

"David Smith" <fl*******@nospam.com> wrote in message
news:2b************************@news.microsoft.com ...
Easy. Use the Ticks property of the static DateTime.Now, as in
DateTime.Now.Ticks.

Hi,

As the title indicated, does anyone know the equivalent of
GetTickCount() function in C/C++ for C#?

The function is supposed to return time elapsed since the
computer/device is booted in milisecond.

By te waym thanks for all the previous help. It is really not easy to
convert current C/C++ codes into the new C# as it is more than
different syntax. I am having headache converting a simple few pages
of C/C++ into C#... At times, I would like to have a simple memcpy in
C#.



Dec 5 '05 #10
Hi,

I just want to correct the fact about wrapping. The
Environment.TickCount indeed is of type int (which is CLS compliant)
instead of uint/DWORD for GetTickCount() (which is non-compliant), but
it is a direct wrapper of GetTickCount() otherwise. The old MSDN
documentation didn't state that it wraps to zero - because it wraps to
int.MinValue. This is corrected in the new MSDN, which states:

The value of this property is derived from the system timer and is
stored as a 32-bit signed integer. Consequently, if the system runs
continuously, TickCount will increment from zero to Int32.MaxValue for
approximately 24.9 days, then jump to Int32.MinValue, which is a
negative number, then increment back to zero during the next 24.9 days.

The standard (Environment.TickCount - tStart > tDelay) test wouldn't
work if the value wrapped to zero.

Just my 2c.

Stoitcho Goutsev (100) [C# MVP] wrote:
David,

DateTime.Now.Ticks will return the time elapsed since Jan 1st 0001 12:00 AM
in tick units where each unit is 100 nanoseconds. Nothing to do with the
time ellapsed since last time the system has started.

The property to look at is System.Environment.TickCount

I'd suggest reading the help for this property carefully because there might
be some gotchas.
Keep in mind that the C++ methods returns the result in DWORD, which is 32
bit unsigned integer. System.Environment.TickCount is Int32, which is 32 bit
signed integer. That means System.Environment.TickCount will wrap around to
zero sooner than the C++ method - Int32 is half smaller that DWORD in terms
of positive numbers.

Dec 6 '05 #11
Stoitcho Goutsev (100) [C# MVP] wrote:
MSDN:
" Remarks
The value of this property is derived from the system timer and is stored as
a 32-bit signed integer. Therefore, the elapsed time will wrap around to
zero if the system is run continuously for 24.9 days.


So even Microsoft don't have faith in their machines being up for 25
days? That's just great! ;)
Dec 6 '05 #12
Stoitcho Goutsev (100) [C# MVP] wrote:
System.Environment.TickCount has nothing to do with DateTime and its
100-nanoseconds accuracy. They are implemented differently and they
expilicitly state this in MSDN:

MSDN:
" Remarks
The value of this property is derived from the system timer and is stored as
a 32-bit signed integer. Therefore, the elapsed time will wrap around to
zero if the system is run continuously for 24.9 days.


Wrap around to 0? I thought a signed in would go to negatives when it
overflowed?

eg. 24.8 -> 24.9 -> -24.9 -> -24.8 ??
(I've never written anything that relies on this "fact", it was just my
understanding!)
Dec 6 '05 #13
Reading carefully you'll see they explain this:
"... The TickCount property handles an overflow condition by resetting its
value ..."

Well regarding the faith in the system.... I don't see much difference.
System working for only 50 days is as bad as system working for 25. :) I
rather think that they use Int32 because usnigned types are not CLS
compliant.

Anyways, as I said 50 is not much better than 25, so I believe if you look
for bigger accuracy you'll be better off with reading *System Up Time*
performance counter.

using System.Diagnostics;
.....
PerformanceCounter upTime = new PerformanceCounter("System","System Up
Time");
// You've got to call this twice. First time it returns 0 and
// second time it returns the real info.
upTime.NextValue();
MessageBox.Show(String.Format(TimeSpan.FromSeconds (upTime.NextValue()).ToString()));

You need to keep in mind the following:
1. Performance counters are part of WMI and as such they are protected by
their own security settings (user rights are privileges). It won't be a
problem for desktop applications, but it might be for web application when
using impersonation.
2. Reading performance counters is not one of the fastest operations and
you might run into performance problems if you do this very often
3. "Syste Up Time" takes into consideration only the time when the system is
running that is the time that the system spend in hibernation does no count.
Thus this counter cannot be used to calculate the exact system boot date and
time.

--

Stoitcho Goutsev (100) [C# MVP]
to zero. The minimum value returned by TickCount is 0.
"Danny Tuppeny" <gr****@dantup.me.uk.remove> wrote in message
news:43***********************@ptn-nntp-reader03.plus.net...
Stoitcho Goutsev (100) [C# MVP] wrote:
System.Environment.TickCount has nothing to do with DateTime and its
100-nanoseconds accuracy. They are implemented differently and they
expilicitly state this in MSDN:

MSDN:
" Remarks
The value of this property is derived from the system timer and is stored
as a 32-bit signed integer. Therefore, the elapsed time will wrap around
to zero if the system is run continuously for 24.9 days.


Wrap around to 0? I thought a signed in would go to negatives when it
overflowed?

eg. 24.8 -> 24.9 -> -24.9 -> -24.8 ??
(I've never written anything that relies on this "fact", it was just my
understanding!)

Dec 6 '05 #14
Stoitcho Goutsev (100) [C# MVP] wrote:
Reading carefully you'll see they explain this:
"... The TickCount property handles an overflow condition by resetting its
value ..."


I thought it probably did, right after posting!
Dec 6 '05 #15
Stefan Simek wrote:
The value of this property is derived from the system timer and is
stored as a 32-bit signed integer. Consequently, if the system runs
continuously, TickCount will increment from zero to Int32.MaxValue for
approximately 24.9 days, then jump to Int32.MinValue, which is a
negative number, then increment back to zero during the next 24.9 days.


Now I'm confused! Does it wrap to MinValue, or reset to 0? :-\
Dec 6 '05 #16
Danny Tuppeny wrote:
Stefan Simek wrote:
The value of this property is derived from the system timer and is
stored as a 32-bit signed integer. Consequently, if the system runs
continuously, TickCount will increment from zero to Int32.MaxValue for
approximately 24.9 days, then jump to Int32.MinValue, which is a
negative number, then increment back to zero during the next 24.9 days.


Now I'm confused! Does it wrap to MinValue, or reset to 0? :-\


It wraps to MinValue. If you think of it in hex, 0x7fffffff is
int.MaxValue, and next is 0x80000000 which is int.MinValue. The counter
is simply continuously incremented without any special cases like
wrapping from 0x7fffffff to 0.

Stefan
Dec 7 '05 #17
Stefan Simek wrote:
Now I'm confused! Does it wrap to MinValue, or reset to 0? :-\


It wraps to MinValue. If you think of it in hex, 0x7fffffff is
int.MaxValue, and next is 0x80000000 which is int.MinValue. The counter
is simply continuously incremented without any special cases like
wrapping from 0x7fffffff to 0.


But what Stoitcho Goutsev posted says MSDN says the opposite! :\
Dec 7 '05 #18
Dany,

The one that I've posted comes from the docs for .NET1.x the one that Stefan
posted is from the .NET2.0 docs. It is possible that they've made one of
these "minor" changes that can make one's life miserable :).
Unfortunately it is hard to test. Even the method is implented internally in
the CLR and reflector tools and ILDasm won't help.

I personally still think that majority of the programmers hasn't switched
yet to VS2005 and .NET 2.0 so if the version is not specified I assume
..NET1.x.
--
Stoitcho Goutsev (100) [C# MVP]

"Danny Tuppeny" <gr****@dantup.me.uk.remove> wrote in message
news:43***********************@ptn-nntp-reader03.plus.net...
Stefan Simek wrote:
Now I'm confused! Does it wrap to MinValue, or reset to 0? :-\


It wraps to MinValue. If you think of it in hex, 0x7fffffff is
int.MaxValue, and next is 0x80000000 which is int.MinValue. The counter
is simply continuously incremented without any special cases like
wrapping from 0x7fffffff to 0.


But what Stoitcho Goutsev posted says MSDN says the opposite! :\

Dec 7 '05 #19
>The one that I've posted comes from the docs for .NET1.x the one that Stefan
posted is from the .NET2.0 docs. It is possible that they've made one of
these "minor" changes that can make one's life miserable :).


No it was the documentation that was incorrect. Even in 1.x it was no
more than a straight call to GetTickCount().
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 7 '05 #20
Mattias,

Here is how the method for getting the tick count is declared in the
Enviroment class:

..method private hidebysig static int32 nativeGetTickCount() cil managed
internalcall

The following is the same method imported for PInvoke

..method public hidebysig static pinvokeimpl("kernel32.dll") int32
GetTickCount() cil managed

*internalcall* from the Environment class means that the method
implementation is provided form the CLR.

How do you know what is the implementation of this method?

In 17 days if I in meanwile don't restart my machine I'll give it a test :-)
--

Stoitcho Goutsev (100) [C# MVP]

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:O8****************@TK2MSFTNGP11.phx.gbl...
The one that I've posted comes from the docs for .NET1.x the one that
Stefan
posted is from the .NET2.0 docs. It is possible that they've made one of
these "minor" changes that can make one's life miserable :).


No it was the documentation that was incorrect. Even in 1.x it was no
more than a straight call to GetTickCount().
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Dec 7 '05 #21
Stoitcho,
How do you know what is the implementation of this method?


By looking at the source code of course. MVPSLP has its advantages.
It's also available in SSCLI, see SystemNative::GetTickCount in
clr\src\vm\comsystem.cpp.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 7 '05 #22
Stoitcho Goutsev (100) [C# MVP] wrote:
The one that I've posted comes from the docs for .NET1.x the one that Stefan
posted is from the .NET2.0 docs.


That makes more sense! :)

I can't believe they'd make such a "breaking change" though, given this:

http://lab.msdn.microsoft.com/Produc...6-cd6886be35b8

;o)
Dec 8 '05 #23
Hmm, Okay I believe you now :-). I should apply for this program. Somehow I
missed when it started.

Thanks,

--
Stoitcho Goutsev (100) [C# MVP]

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uj***************@TK2MSFTNGP09.phx.gbl...
Stoitcho,
How do you know what is the implementation of this method?


By looking at the source code of course. MVPSLP has its advantages.
It's also available in SSCLI, see SystemNative::GetTickCount in
clr\src\vm\comsystem.cpp.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Dec 8 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Leader | last post by:
Hi All, I am facing a problem with a sql what i used in MS Access but its not returning the same result in MS Sql Server 2000. Here i am giving the sql: SELECT TOP 3 format( MY_DATE, "dddd mm,...
4
by: | last post by:
I have a stored procedure using a char(10), I need to specify that param in c#. What c# data type is can I use? I've looked around online for equivalents between sql types and c# types and...
3
by: tanya foster | last post by:
Hello, I am re-writing a visual basic .net application(visual studio 2003) in an asp.net application(visual studio 2005). The vb.net application relied on a treeview and hence, treenodes. The...
8
by: COHENMARVIN | last post by:
I'm a VB programmer learning C#. One thing I don't know is how to test whether an object is 'null' or not. There is no 'null' keyword in C#. Why is that? CM
1
by: vasuvandla | last post by:
Hi I am working in Conversion project from VB6.0 to C#(2005). But i am facing lot of problems with object which are in VB6 and which are not available in C#. Could You please help out me what is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.