tags:

views:

578

answers:

5

hi,

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!

+2  A: 

Make sure you start it by calling timer1.Start()

Isak Savo
+1 I find that a lot of folks don't do this :)
PieterG
Yeah, I've been bitten by this myself a few times
Isak Savo
.Enabled=true should be enough.
Henk Holterman
Yeah it's "enough".. In fact .Start() is the exact same thing as setting enabled to True :)
Isak Savo
A: 

Did you start the timer?

erasmus
+1  A: 

don't System.Windows.Forms.Timer timer start on .enabled... anyway.. i've just got it to work... i copied the visual studio generadted code form WINFORMNAME.designer.cs to WINFORMNAME.cs... i don't know how and why but it worked...

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Enabled = true;
timer.Interval = 1000; 
timer.Tick += new EventHandler(timer_Tick); 
        void timer_Tick(object sender, EventArgs e)
        {
//do something
        }

thanks everybody for the answers!

Andrej
A: 

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?

Wes Long
A: 

To answer your last question, it's not allowed to manipulate controls that are created on another thread. You can invoke via delegates.

Tom