Analysis of the .NET timers with 1ms tick size:
- System.Timers.Timer
- System.Threading.Timer
- Multimedia Timer imported from
winmm.dll
- TwinCAT 3. Soft Real-Time System
- Dedicated Thread
If you don't have Jupiter Notebooks installed locally, just use online Jupyter NBViewer
System.Timers.Timer
andSystem.Threading.Timer
are limited with 15.6 ms by default.Multimedia Timer
. Always stable but legacy.- TwinCAT3 notifications. Not good at client side.
- Independed threads. Not stable at heavy load.
Conclusion. Should use legacy Multimedia Timer
or modern real-time systems like TC3.
-
Acquiring high-resolution time stamps -- 31-05-2018
-
Results of some quick research on timing in Win32 by Ryan Geiss -- 16 August 2002 (...with updates since then)
-
Real-Time Systems with Microsoft Windows CE 2.1 -- 06/29/2006
-
docx
Timers, Timer Resolution, and Development of Efficient Code -- June 16, 2010
Loads all cpu's cores w/ 100%
while (!token.IsCancellationRequested) {
Parallel.For( 0, Environment.ProcessorCount, ( i ) => {
double a = 1000.0;
for (int j = 0; j < 10_000_000; j++) {
if (token.IsCancellationRequested)
break;
a /= j * (i + 1);
}
} );
}
See CpuBurner.cs
Legacy functions from early versions of Windows
[DllImport( "winmm.dll", SetLastError = true, EntryPoint = "timeSetEvent" )]
internal static extern UInt32 TimeSetEvent( UInt32 msDelay, UInt32 msResolution, MultimediaTimerCallback callback, ref UInt32 userCtx, UInt32 eventType );
[DllImport( "winmm.dll", SetLastError = true, EntryPoint = "timeKillEvent" )]
internal static extern void TimeKillEvent( UInt32 uTimerId );
System tick and PLC-Runtime both are 1 millisecond.
PROGRAM MAIN
VAR
tick: BOOL;
END_VAR
tick := NOT tick;
END_PROGRAM
AdsClient client = new AdsClient();
client.Connect( AmsPort.R0_RTS + 1 );
client.AdsNotification += (s,e) => DoTick();
var notiSets = new NotificationSettings( AdsTransMode.OnChange, 1, 0 );
var h_tick = client.AddDeviceNotification( "MAIN.tick", 1, notiSets, null );