0

I am trying to stop the timer when it is not in use and start it at a later timer when I need it. If I try to stop the timer using timer.Stop() it says 'Object reference not set to an instance of an object.'. If I Try to start the timer outside the initialise function I get the same error message even though my timer is global.

How can I control the timer outside the Initialise function but call the same methods? C#:

public class SmartDispatcherTimer : DispatcherTimer
{
    public SmartDispatcherTimer()
    {

        base.Tick += SmartDispatcherTimer_Tick;
    }

    async void SmartDispatcherTimer_Tick(object sender, EventArgs e)
    {
        if (TickTask == null)
        {
            Console.WriteLine("No task set!");
            return;
        }

        if (IsRunning && !IsReentrant)
        {
            // previous task hasn't completed
            Console.WriteLine("Task already running");
            return;
        }

        try
        {
            // we're running it now
            IsRunning = true;
            Console.WriteLine("Running Task");

            await TickTask.Invoke();
            Console.WriteLine("Task Completed");

        }
        catch (Exception ex)
        {
            Console.WriteLine("Task Failed");
        }
        finally
        {
            // allow it to run again
            IsRunning = false;
        }
    }

    public bool IsReentrant { get; set; }
    public bool IsRunning { get; private set; }

    public Func<Task> TickTask { get; set; }
}

private SmartDispatcherTimer timer;

private void Initialise()
{
    try
    {
        timer = new SmartDispatcherTimer()
        {
            IsReentrant = true,
            Interval = TimeSpan.FromSeconds(1),
            TickTask = async () => { await GetData(); }
        };

        timer.Start();

    }
    catch (Exception ex)
    {
        MessageBox.Show("Task Failed");
    }
}
Decoder94
  • 1,488
  • 2
  • 15
  • 37
  • Are your SmartDispatcherTimer instance and Initialise method inside of a different class? From this code sample, it looks like they aren't within the scope of any class, which would be a no-no. If you need a single "global instance", you could look into the singleton pattern. – Dave Smash Nov 08 '18 at 16:27
  • @ElementalPete Yes The instance and the Initialise method is the same class that's why I don't understand why I am getting a NullReference. I don't understand why I get a null reference even if I do `timer.Start()` in the same class but a different method using the same the same instance of `timer` but it works fine in the initialise method – Decoder94 Nov 08 '18 at 16:35
  • You are initializing it in a try-catch, meaning that if it failed to initialize, you would see "Task Failed" in a message box, but it would remain null. Assuming that you aren't seeing that, I would set a breakpoint in Initialise to make sure that code is actually being run - you need to explicitly call Initialise in your class constructor or someplace like that. Without seeing the class where you are using the timer, it is tough to see the bug you are running into... – Dave Smash Nov 08 '18 at 16:42

0 Answers0