VB - calculate the period of millisecond accurately but with suspension timer

WONG Tony 161 Reputation points
2021-09-10T02:46:39.153+00:00

The period is calculated as below
AnswerTime1 = now() ' start of event
AnswerTime2 = now() ' end of event - suppose at 14.9seconds
There is a timer to restrict it in 15 seconds
Dim TimeDiff As TimeSpan = AnswerTime2 - AnswerTime1
it works flawlessly, never go beyond 15 seconds

However it allows suspensions in the middle of 15 seconds period, the suspension period will be deducted.
when suspension starts, the Timer.stop, vice versa

SuspTime1 = Now() ' start of Suspension
SuspTime2 = Now() ' end of Suspension
SuspTimeSpan = SuspTime2 - SuspTime1 + SuspTimeSpan
Dim TimeDiff As TimeSpan = AnswerTime2 - AnswerTime1 - SuspTimeSpan

Timer is running at 1000 interval

However, it returns greater than 15 seconds. it is worser if more suspensions in middle

any advice? Thanks a lot.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,624 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,661 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,526 Reputation points
    2021-09-10T02:59:53.393+00:00

    Most versions of Windows are not what is called a Real Time Operating System. A very technical description of that is at Windows and Real-Time – OSR. I do not know if that is the cause but if it is then you cannot expect precision from Windows, at least not from most versions of it, such as Windows 10 and 11. If your requirements are time-critical then you can improve accuracy using a device driver but even then you are not guaranteed accuracy.

    0 comments No comments