Search code examples
c#wpftimerasync-await

Async System.Threading.Timer sometimes throwing null exception


I am trying to execute a code asynchronously after a certain interval of time and want to check again only after I am done with the code execution to prevent overlapping. For that I am using the following variables and timer code.

private System.Threading.Timer _timer;
private int _intervalInMilliSec = 10000;

_timer = new System.Threading.Timer(async (o) =>
{
    // Stop the timer;
    _timer.Change(-1, -1);

    // awaited api call goes here

    // start timer again (BeginTime, Interval)
    _timer.Change(_intervalInMilliSec, _intervalInMilliSec);
}, null, 0, _intervalInMilliSec);

Most of the times it is working fine but sometimes rarely I started getting the following exception at the line _timer.Change(-1, -1);

Object reference not set to an instance of an object

At that point "o" is showing as null. Please let me know what I am doing wrong. Thanks


Solution

  • 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.