1
Answer

Timer Not Working in Derived Class.

Brandon Lewis

Brandon Lewis

16y
2.1k
1
I have a little issue with a timer not working here, which is quite the bother! lol. Its got be baffled, as there are no compiler errors,
no crashes, and no logic errors I can see. It just simply doesnt trigger the timer tick event handler for the derived ReadStatus class. Any ideas?
I tried to make it as simple as possible (in the real application its a much larger amount of code!) so itd be easy to read. Check it out and
see if you can come up with anything!

public class SystemStatus
{
    protected Timer warningTimer = new Timer()

    public SystemStatus()
    {
        warningTimer.Interval = 500;
        warningTimer.Tick += new EventHandler(warningTimer_Tick);
    }

    public virtual void SetLevel(int level)
    {
        if(level == 1 || level == 2)
        {
            if(!warningTimer.Enabled)
            {
                warningTimer.Enabled = true;
                warningTimer.Start();
            }
        }
        else
        {
            if(warningTimer.Enabled)
            {
                warningTimer.Stop();
                warningTimer.Enabled = false;
            }

            //Perform some irrelevant updating stuff
        }
    }

    protected virtual void warningTimer_Tick(object sender,
        EventArgs e)
    {
        //Do some irrelevant stuff
    }
}


public class ReadStatus : SystemStatus
{
    public ReadStatus() : base()
    {

    }

    public override void SetLevel(int level)
    {
        if(level == 1)
        {                
            if(!warningTimer.Enabled)
            {
                warningTimer.Enabled = true;
                warningTimer.Start();
            }
        }
        else
        {
            if(warningTimer.Enabled)
            {
                warningTimer.Stop();
                warningTimer.Enabled = false;
            }

            //Do some class specific updating here.    
        }
    }

    protected override void warningTimer_Tick(object sender,
        EventArgs e)
    {
        //Do some different stuff specific to this class
    }
}


For some reason, when I start the timer in an instance of the ReadStatus class, the warningTimer_Tick event handler never gets triggered even though
if I set up break points, I can clearly see that the timer is being started. Is this some sort of an inheritance glitch??? Thanks!
Answers (1)