开发者

C# timer won't tick

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 s开发者_开发百科elect 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!


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?


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!


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


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


I've ran into this before. I was enabling the timer before setting the interval. Swapping the two lines around made it work.


I had the same problem as Andrej and I've tried several solutions I found on web.

I had time limit for each form in my program (countdown) and Here's How I Solved It :

I used computer engineering solution. turn it off and on again!! :)) I mean I don't know why but it works!!

first of all I deleted all timers from .cs and design then I added a timer in each form and put the different name for timer and text box and wrote the same code one by one.

I just used timer.start() and Timer.stop()

private void Form7_Load_1(object sender, EventArgs e)
    {
        timer7.Start();
    }

    int duration = 30;

    private void timer7_Tick(object sender, EventArgs e)
    {
        duration--;
        textBox7.Text = duration.ToString(); // to show the countdown 

        if (duration == 0)
        {
            timer7.Stop();
            MessageBox.Show("You ran out of time. Please go to the next page");

            this.Hide();
            Form8 f8 = new Form8();
            f8.ShowDialog();
        }
    }

hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜