C# 计时器不会打勾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13412145/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timer won't tick
提问by cpdt
I have a Windows.Forms.Timerin my code, that I am executing 3 times. However, the timer isn't calling the tick function at all.
Windows.Forms.Timer我的代码中有一个,我正在执行 3 次。但是,计时器根本没有调用滴答函数。
private int count = 3;
private timer;
void Loopy(int times)
{
    count = times;
    timer = new Timer();
    timer.Interval = 1000;
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
    count--;
    if (count == 0) timer.Stop();
    else
    {
        // Do something here
    }
}
Loopy()is being called from other places in the code.
Loopy()正在从代码中的其他地方调用。
采纳答案by Florin Petriuc
Try using System.Timers instead of Windows.Forms.Timer
尝试使用 System.Timers 而不是 Windows.Forms.Timer
void Loopy(int times)
{
    count = times;
    timer = new Timer(1000);
    timer.Enabled = true;
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
    timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    throw new NotImplementedException();
}
回答by Mark Hall
I am not sure what you are doing wrong it looks correct, This code works: See how it compares to yours.
我不确定你做错了什么,它看起来是正确的,这段代码有效:看看它与你的相比如何。
public partial class Form1 : Form
{
    private int count = 3;
    private Timer  timer;
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        Loopy(count);
    }
    void Loopy(int times)
    {
        count = times;
        timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }
    void timer_Tick(object sender, EventArgs e)
    {
        count--;
        if (count == 0) timer.Stop();
        else
        {
            //
        }
    } 
}
回答by Dmitry Ledentsov
回答by user3006708
If the method Loopy() is called in a thread that is not the main UI thread, then the timer won't tick. 
If you want to call this method from anywhere in the code then you need to check the InvokeRequiredproperty. So your code should look like (assuming that the code is in a form):
如果在不是主 UI 线程的线程中调用 Loopy() 方法,则计时器不会计时。如果要从代码中的任何位置调用此方法,则需要检查该InvokeRequired属性。所以你的代码应该看起来像(假设代码是一种形式):
        private void Loopy(int times)
        {
            if (this.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    Loopy(times);
                });
            }
            else
            {
                count = times;
                timer = new Timer();
                timer.Interval = 1000;
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }
        }
回答by sumith madhushan
If you are using Windows.Forms.Timer then should use something like following.
如果您使用的是 Windows.Forms.Timer,则应使用以下内容。
//Declare Timer
private Timer _timer= new Timer();
void Loopy(int _time)
{
    _timer.Interval = _time;
    _timer.Enabled = true;
    _timer.Tick += new EventHandler(timer_Elapsed);
    _timer.Start();
}
void timer_Elapsed(object sender, EventArgs e)
{
    //Do your stuffs here
}

