Help with GetTickCount

Posts 17 of 7 · Page 1 of 1
Help with GetTickCount
i know thats c# but it uses a api from c++ which is GetTickCount
Code:
class AW
    {
        [DllImport("kernel32.dll")]
        static extern int GetTickCount();
        static void Main()
        {
            int timer;

            int interval = 20;

            int start = GetTickCount();
        
            for (timer = 0; timer < 5000; timer += interval)
             {
                 Console.WriteLine("sum1337text");
                 Thread.Sleep(interval);

             }
            
            int end = GetTickCount();
            
            

        }
so i didnt got it how this gettickcount works can someone show me how i can replace the sleep with it?
GetTickCount

"Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days"

So it's quite simple;
Code:
int tickStart = GetTickCount(); // Right now.
int tickEnd = tickStart + 5000; // 5 seconds into the future

while(GetTickCount() < tickEnd)
{
    // Do nothing, although it's better to Sleep(0) in here to not waste CPU time.
}

MessageBox.Show("Hurraiiii, 5 seconds have passed.");
Quote Originally Posted by Adsvunu View Post
i know thats c# but it uses a api from c++ which is GetTickCount
Code:
class AW
    {
        [DllImport("kernel32.dll")]
        static extern int GetTickCount();
        static void Main()
        {
            int timer;

            int interval = 20;

            int start = GetTickCount();
        
            for (timer = 0; timer < 5000; timer += interval)
             {
                 Console.WriteLine("sum1337text");
                 Thread.Sleep(interval);

             }
            
            int end = GetTickCount();
            
            

        }
so i didnt got it how this gettickcount works can someone show me how i can replace the sleep with it?
Overall, GetTickCount is used to count how many milliseconds have passed since the system was started. What Hell_Demon did for you is clean up your code.

First , he declared 2 variables: tickStart and tickEnd.
-Basically, a point where the timer starts and ends.
-He set the timer to 5 seconds (5000 milliseconds).

Second, he made a very simple statement: "Do the following if it has been less than 5 seconds." 'while(GetTickCount() < tickEnd)'
-Once the 5 seconds are reached, end the timer (tickEnd) and display a message (MessageBox.Show("Hurraiiii, 5 seconds have passed."))

Your code was a bit messy because you added an unnecessary timer (GetTickCount already calculates the time for you) and had an overall logical error.
Also (I am not 100% sure), 'int end = GetTickCount();' will cause an unintended (I am assuming you did not want this to happen) loop/error, since you are telling the computer to calculate the time once again.
Quote Originally Posted by Gill Bates View Post
Overall, GetTickCount is used to count how many milliseconds have passed since the system was started. What Hell_Demon did for you is clean up your code.

First , he declared 2 variables: tickStart and tickEnd.
-Basically, a point where the timer starts and ends.
-He set the timer to 5 seconds (5000 milliseconds).

Second, he made a very simple statement: "Do the following if it has been less than 5 seconds." 'while(GetTickCount() < tickEnd)'
-Once the 5 seconds are reached, end the timer (tickEnd) and display a message (MessageBox.Show("Hurraiiii, 5 seconds have passed."))

Your code was a bit messy because you added an unnecessary timer (GetTickCount already calculates the time for you) and had an overall logical error.
Also (I am not 100% sure), 'int end = GetTickCount();' will cause an unintended (I am assuming you did not want this to happen) loop/error, since you are telling the computer to calculate the time once again.
I got it but seems that doesnt worth using it in c# since its slower than sleep, but in c++ its fine
Or simply use Environment.TickCount
Quote Originally Posted by Zaczero View Post
Or simply use Environment.TickCount
which is pretty much the same as

Code:
static int TickCount
{
    get
    {
        return GetTickCount();
    }
}
Quote Originally Posted by Silent View Post


which is pretty much the same as

Code:
static int TickCount
{
    get
    {
        return GetTickCount();
    }
}
"simply use"
Posts 17 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?