user3545411
user3545411

Reputation: 13

Sleep specific Ticks

I need a function that waits for a specified number of DateTime.Ticks (about 155 000). Unfortunately, it works with the following function not really (waited about 300 000 ticks):

private void sleep_ticks(long t)
{
    long _start = DateTime.Now.Ticks + t;
    int i = 0;

    do
    {
        i++;
        Console.WriteLine((DateTime.Now.Ticks - _start) + " => " + i);
        if (DateTime.Now.Ticks >= _start)
            break;
    } while (true);

    Console.WriteLine((DateTime.Now.Ticks - _start) + " < " + t + " -- " + i);
    Console.ReadLine();
}

I added some debug output :-) The last value would have to be close to 0.

Here is the output for sleep_ticks (1):

-1 => 1; -1 => 2; -1 => 3; -1 => 4; -1 => 5; -1 => 6; -1 => 7; -1 => 8; -1 => 9; -1 => 10; 
-1 => 11; -1 => 12; -1 => 13; -1 => 14; -1 => 15; -1 => 16; -1 => 17; -1 => 18; -1 => 19; 
-1 => 20; -1 => 21; -1 => 22; -1 => 23; -1 => 24; -1 => 25; -1 => 26; -1 => 27; -1 => 28; 
-1 => 29; 156262 < 1 -- 29;

Does anyone have any idea why this might be, or how to fix it?

Thanks and regards Robert

Upvotes: 1

Views: 4806

Answers (4)

580
580

Reputation: 480

"I need a function that waits for a specified number of DateTime.Ticks." This is not possible.

Use System.Threading.Thread.Sleep(int millisecondsTimeout) for waiting accuracy gauged in milliseconds.

// Puts the current thread to sleep for 15 milliseconds.
System.Threading.Thread.Sleep(15);

It should be noted regarding the accepted answer (https://stackoverflow.com/a/23134395/3076020) that Thread.Sleep(TimeSpan) disregards fractional milliseconds.

"This overload of Sleep uses the total number of whole milliseconds in timeout. Fractional milliseconds are discarded." As documented here: https://msdn.microsoft.com/en-us/library/274eh01d(v=vs.110).aspx

This means that half a millisecond will be disregarded in the minimum time period to wait when providing Thread.Sleep 155000 ticks utilizing a TimeSpan. There are 10000 ticks in a single millisecond, therefore 155000 ticks = 15.5 milliseconds. While this may not have much impact in this scenario where 15 milliseconds is being waited. It may have an impact in a scenario where ticks or some measure more accurate than milliseconds is actually desired. This is why in this case I suggest for clarity using the integer (millisecond) overload.

If using milliseconds is not applicable I suggest looking into spinning the thread. Meaning keeping it active/busy repeatedly checking a suitably high-resolution timer between calls. Even this is prone to giving inaccurate results as the operating system may schedule another thread of higher priority, thus skewing the timing.


Be aware Thread.Sleep is the minimum time to keep the thread off of the scheduler, kept from processing, sleeping. The thread may end up "waiting" to resume processing longer than specified. This answer provides a bit of clarity on why Thread.Sleep isn't always precisely the time specified. https://stackoverflow.com/a/6254753/3076020

Reference information regarding that a single millisecond is 10,000 ticks. I believe this is standard in .NET and cannot be changed. https://msdn.microsoft.com/en-us/library/system.timespan.tickspermillisecond(v=vs.110).aspx

Upvotes: 4

SMA
SMA

Reputation: 74

private void TicksDrivenSleepingThread(long Ticks) { int imillisecondsTimeout;

        //  http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx
        //A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond. 
        //Convert Ticks to their Millseconds Equivalen
        imillisecondsTimeout = Convert.ToInt32((Ticks / 10000));

        while (true) 
        {

            //Wait
            Thread.Sleep(imillisecondsTimeout);
            //Do What Needs Doing
        }


    }

Upvotes: 0

BamBam
BamBam

Reputation: 23

This is command that pauses the system, you should be able to implement it into your code to make it do what you need it too.

System.Threading.Thread.Sleep(100);

Where i have 100 you can set it to your DateTime.Now.Ticks and it should still work.

Upvotes: 0

Ian
Ian

Reputation: 34549

Use Thread.Sleep

System.Threading.Thread.Sleep(new TimeSpan(155000));

Upvotes: 3

Related Questions