C# timer won't tick

c#, timer, winforms

Solution

I have found that if I stop the timer from a non-UI thread, then start it again, I lose the event hook.

I don't know what the "proper" answer is, but this worked quite well for me:

public class BetterTimer : System.Windows.Forms.Timer
{
    public BetterTimer():base()        
    { base.Enabled = true; }

    public BetterTimer(System.ComponentModel.IContainer container) : base(container) 
    { base.Enabled = true; }

    private bool _Enabled;
    public override bool Enabled
    {
        get { return _Enabled; }
        set { _Enabled = value; }
    }

    protected override void OnTick(System.EventArgs e)
    { if (this.Enabled) base.OnTick(e); }
}

Three things to this approach:

1) By overriding the constructors, I ensure that the base timer is enabled from the beginning.

2) By Overrideing "Enabled," I never let the base timer become disabled, but the interface doesn't change.

3) By overriding "OnTick," I let the overridden Enabled property decide if the event should fire or not.

Start() and Stop() work by setting true and false to the Enabled property, respectively.

BTW - does anyone know why the event never fires (or is disconnected?) when the timer is stopped/disabled from a non-UI thread?

Problem

i have a strange problem... I've been going out of my mind for the past couple of hours... the timer i put in my winform code (from the toolbar) won't tick... I have timers on a couple of forms in my program, they all work fine... I try to do exactly the same it this it won't tick... I select it, drag it on to a form, enable it, set interval and handle the tick event... and nothing happens... i even tried putting random code like messagebox.show in the tick event just to see if anything happens, and nothing!!! as I said, a have a couple of more timer in my program (on other forms, not in the one i'm trying to put this timer) and they all work fine... any suggestions? thanks in advance!

Original source