Quantcast
Channel: Visual Basic Tips and Tricks
Viewing all articles
Browse latest Browse all 2212

Stopwatch: una questione di millisecondi...

$
0
0

 

Buongiorno, utilizzo questo codice per inviare un pacchetto RTP ogni 20 msec:

 

        Dim tick As New Stopwatch
        If (Stopwatch.IsHighResolution) Then
            l.WriteLine("Operations timed using the system's high-resolution performance counter.")
        Else
            l.WriteLine("Operations timed using the DateTime class.")
        End If
        Dim frequency As Long = Stopwatch.Frequency
        l.WriteLine($"Timer frequency in ticks per second = {CStr(frequency)}", clsLogger.sign._PRINT)
        Dim nanosecPerTick As Long = (1000 * 1000 * 1000) / frequency
        l.WriteLine($"Timer is accurate within {CStr(nanosecPerTick)} nanoseconds", clsLogger.sign._PRINT)
        While go
            tick.Start()
            If (tick.ElapsedMilliseconds >= 19) Then    ' metto 19 per recuperare il millisecondo di attesa
                sendPacket_Elapsed()
                tick.Restart()
            End If
            System.Threading.Thread.Sleep(1)            ' questo mi ottimizza di moltissimo l'utilizzo della CPU, se non lo metto sale al 25% fisso (occupa un core intero)
        End While

 

Girando su un PC, i pacchetti escono correttamente ogni 20 msec, girando su di un altro escono ogni 31 msec...

 

Premesso che su entrambe il PC la proprietà IsHighResolution viene data come True.

 

Qualcuno mi sa dare una spiegazione?

 

Grazie

 

 


Viewing all articles
Browse latest Browse all 2212