Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# timer won't tick

Tags:

c#

winforms

timer

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!

like image 900
Andrej Avatar asked Mar 19 '10 06:03

Andrej


4 Answers

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?

like image 58
Wes Long Avatar answered Oct 20 '22 00:10

Wes Long


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

like image 21
Tom Avatar answered Oct 20 '22 01:10

Tom


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!

like image 45
Andrej Avatar answered Oct 20 '22 01:10

Andrej


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

like image 38
Isak Savo Avatar answered Oct 19 '22 23:10

Isak Savo