CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2010
    Posts
    3

    [RESOLVED] Timing problem in C#

    Hi guys,

    My OS is Windows 7 32-bit, and I'm using Visual Studio 2008.

    In my C# program I use function Thread.Sleep(5) to cause my thread to sleep in 5(ms), and then I print the elapsed time to the screen. My code snippet:
    Code:
    for (int i = 0; i < 20; i++)
                {
                    myTimer.Start();
                    Thread.Sleep(5);
                    myTimer.Stop();
                    dTime = myTimer.Duration;
                    Console.WriteLine("{0}\n", dTime);
                    // Thread.Sleep(1000);
                }
                Thread.Sleep(20000);
    The strange thing is that: Depending on whether or not I comment the line Thread.Sleep(1000) (as you can see from the above code snippet), the Thread.Sleep(5) function may not work accurately. If I uncomment the line, then the function seems to work more accurately than when I comment it. Please have a look at the two pictures I attached herewith to see the results more clearly.

    So, my question is what causes the inaccuracy in the second case? Is there any thing I can do to improve the accuracy in that case?

    Thank you for reading my question.

    Kakashi
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Timing problem in C#

    You can't really expect these things to be absolutely precise on a non RTOS. Do you really think there is a problem with the accuracy you see here? What are you actually trying to accomplish? The code doesn't look like it is doing anything meaningful.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Timing problem in C#

    If the timer you are using is based on a windows timer, then I believe the thread where it was created needs to remain unblocked.

  4. #4
    Join Date
    Jun 2010
    Posts
    85

    Re: Timing problem in C#

    Arjay is correct, deep down on the inside the timer send a WM_TIMER message to your WndProc, and if your thread is blocked then it can not process the message in a timely manner.
    --------------------------------------------------------------------------------------------------------------------------
    Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.

  5. #5
    Join Date
    Jul 2010
    Posts
    3

    Re: Timing problem in C#

    Hi guys,

    Thank you for your answer.

    The timer I used to measure the elapsed time is a high performance timer with an accuracy in the microseconds scale. Its source code can be downloaded from the following address:
    Code:
    http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx
    So I believe that the results of the measurements are correct.

    @BigEd781: This code just does a simple task: start a timer, sleep the thread in 5 (ms) and then stop the timer and then get the elapsed time. Its sole purpose is to measure the period of time during which the thread really 'sleeps', in order to see whether or not Thread.Sleep(5) is accurate.

    It doesn't seem that the thread is blocked in the second case, because the sleep time is smaller than 5 (ms). Do you have any idea about this?

    Regards,

    Kakashi
    Last edited by hatake_kakashi; July 16th, 2010 at 11:48 PM.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Timing problem in C#

    Quote Originally Posted by hatake_kakashi View Post
    Do you have any idea about this?
    Yes, Sleep isn't that accurate. When the OS thread scheduler runs, it hands out time slices to threads based on priority. It runs realtime, high, normal, below normal threads all in that order. If any realtime threads need to run, it runs them to completion, then it runs the high priority threads and so on.

    When you call Sleep, the thread scheduler knows to skip over that thread until the sleep expires.

    Now given how the thread scheduler works with regard to priority, it doesn't mean that just because the sleep has expired on your thread, it will immediately get to run.

    Raising your thread priority to High may help, but you would still be sharing time slices with other high pri threads, so there wouldn't be any guarantee that your results would be consistant.

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: Timing problem in C#

    Sleep is accurate to whatever the system clocks accuracy is. Unless there's a bug in the implementation you are guaranteed that your thread will sleep *at the minimum* the time you specify. However it may sleep a bit longer if your system is busy processing other threads.

    Of course reading the documentation will get you right into the nitty gritty of it all and tell you the limits of what's possible:
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    I'd always recommend reading the docs when you have questions like this. The can sometimes answer the question much better than your average punter
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  8. #8
    Join Date
    Jul 2010
    Posts
    3

    Re: Timing problem in C#

    Quote Originally Posted by Mutant_Fruit View Post
    Sleep is accurate to whatever the system clocks accuracy is. Unless there's a bug in the implementation you are guaranteed that your thread will sleep *at the minimum* the time you specify. However it may sleep a bit longer if your system is busy processing other threads.

    Of course reading the documentation will get you right into the nitty gritty of it all and tell you the limits of what's possible:
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    I'd always recommend reading the docs when you have questions like this. The can sometimes answer the question much better than your average punter
    The second link you provided answers my question perfectly. Thank you very much.

    Regards,

    Kakashi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured