I am upgrading a C# application from Visual Studio 2010 to 2019 and changing from .net to core so I can run in a container. I'm having a timing problem. The 2010 version drifts and the 2019 version does not. I'm using the Timer class and it would seem the implementation has changed between versions.
2010
#region Assembly mscorlib.dll, v4.0.30319
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll
#endregion
2019
#region Assembly System.Threading.Timer, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\3.1.0\ref\netcoreapp3.1\System.Threading.Timer.dll
#endregion
I've confirmed this with the following test code
using System;
using System.Threading;
namespace TimerTest
{
class Program
{
private Timer t;
private void Tick(object state)
{
Console.Out.WriteLine(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));
}
private void Test()
{
t = new Timer(new TimerCallback(Tick), null, 0, 1000);
}
static void Main(string[] args)
{
new Program().Test();
Console.ReadKey();
}
}
}
2010 Output
02/04/2021 12:10:26.248 AM
02/04/2021 12:10:27.264 AM
02/04/2021 12:10:28.280 AM
02/04/2021 12:10:29.296 AM
02/04/2021 12:10:30.311 AM
02/04/2021 12:10:31.311 AM
02/04/2021 12:10:32.312 AM
02/04/2021 12:10:33.313 AM
02/04/2021 12:10:34.328 AM
02/04/2021 12:10:35.329 AM
02/04/2021 12:10:36.330 AM
02/04/2021 12:10:37.331 AM
02/04/2021 12:10:38.332 AM
02/04/2021 12:10:39.332 AM
02/04/2021 12:10:40.348 AM
02/04/2021 12:10:41.363 AM
02/04/2021 12:10:42.363 AM
02/04/2021 12:10:43.379 AM
02/04/2021 12:10:44.381 AM
02/04/2021 12:10:45.381 AM
02/04/2021 12:10:46.396 AM
02/04/2021 12:10:47.412 AM
02/04/2021 12:10:48.428 AM
02/04/2021 12:10:49.443 AM
02/04/2021 12:10:50.444 AM
02/04/2021 12:10:51.446 AM
02/04/2021 12:10:52.446 AM
02/04/2021 12:10:53.447 AM
02/04/2021 12:10:54.448 AM
02/04/2021 12:10:55.450 AM
02/04/2021 12:10:56.451 AM
02/04/2021 12:10:57.452 AM
02/04/2021 12:10:58.467 AM
02/04/2021 12:10:59.468 AM
02/04/2021 12:11:00.469 AM
02/04/2021 12:11:01.470 AM
02/04/2021 12:11:02.486 AM
02/04/2021 12:11:03.487 AM
02/04/2021 12:11:04.488 AM
02/04/2021 12:11:05.503 AM
02/04/2021 12:11:06.504 AM
02/04/2021 12:11:07.520 AM
02/04/2021 12:11:08.520 AM
02/04/2021 12:11:09.520 AM
02/04/2021 12:11:10.536 AM
02/04/2021 12:11:11.537 AM
02/04/2021 12:11:12.553 AM
02/04/2021 12:11:13.553 AM
02/04/2021 12:11:14.569 AM
02/04/2021 12:11:15.584 AM
02/04/2021 12:11:16.585 AM
02/04/2021 12:11:17.585 AM
02/04/2021 12:11:18.586 AM
2019
02/04/2021 12:09:17.286 AM
02/04/2021 12:09:18.289 AM
02/04/2021 12:09:19.290 AM
02/04/2021 12:09:20.288 AM
02/04/2021 12:09:21.290 AM
02/04/2021 12:09:22.275 AM
02/04/2021 12:09:23.276 AM
02/04/2021 12:09:24.276 AM
02/04/2021 12:09:25.276 AM
02/04/2021 12:09:26.277 AM
02/04/2021 12:09:27.276 AM
02/04/2021 12:09:28.276 AM
02/04/2021 12:09:29.276 AM
02/04/2021 12:09:30.287 AM
02/04/2021 12:09:31.276 AM
02/04/2021 12:09:32.277 AM
02/04/2021 12:09:33.277 AM
02/04/2021 12:09:34.275 AM
02/04/2021 12:09:35.276 AM
02/04/2021 12:09:36.276 AM
02/04/2021 12:09:37.277 AM
02/04/2021 12:09:38.276 AM
02/04/2021 12:09:39.277 AM
02/04/2021 12:09:40.275 AM
02/04/2021 12:09:41.277 AM
02/04/2021 12:09:42.275 AM
02/04/2021 12:09:43.276 AM
02/04/2021 12:09:44.276 AM
02/04/2021 12:09:45.276 AM
02/04/2021 12:09:46.276 AM
02/04/2021 12:09:47.277 AM
02/04/2021 12:09:48.276 AM
02/04/2021 12:09:49.276 AM
02/04/2021 12:09:50.276 AM
02/04/2021 12:09:51.276 AM
02/04/2021 12:09:52.276 AM
02/04/2021 12:09:53.276 AM
02/04/2021 12:09:54.276 AM
02/04/2021 12:09:55.275 AM
02/04/2021 12:09:56.276 AM
02/04/2021 12:09:57.275 AM
02/04/2021 12:09:58.276 AM
02/04/2021 12:09:59.276 AM
02/04/2021 12:10:00.276 AM
02/04/2021 12:10:01.277 AM
02/04/2021 12:10:02.276 AM
02/04/2021 12:10:03.277 AM
You can see the 2010 drifts from .248 to .586 after a minute and the 2019 settles to .276 after a few seconds. The 2019 version is probably better in the general case, but I need to maintain the timing characteristics from 2010 in 2019.
I'm considering adding the drift manually in the 2019 version, but I'm afraid of other changes they made to the class.
Is there any way to request the older implementation of the Timer object in the newer version?
Thanks!