I don't know why you get the NullReferenceException
in your code, but I suggest to scrap the Timer
approach altogether and replace it with a simpler Task.Delay in a loop:
var apiCallCts = new CancellationTokenSource();
var apiCallTask = Task.Run(async () =>
{
while (true)
{
var delayTask = Task.Delay(TimeSpan.FromSeconds(10), apiCallCts.Token);
await SomeApiCallAsync();
await delayTask;
}
});
And at the app shutdown:
apiCallCts.Cancel();
The above solution has a different behavior in case of an API failure. Instead of crashing the app, will silently stop calling the API. This is something that you'll have to consider.