简体   繁体   English

计时器不会打勾

[英]Timer won't tick

I have a Windows.Forms.Timer in my code, that I am executing 3 times.我的代码中有一个Windows.Forms.Timer ,我正在执行 3 次。 However, the timer isn't calling the tick function at all.但是,计时器根本没有调用滴答函数。

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()是从代码中的其他地方调用的。

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();
}

If the method Loopy() is called in a thread that is not the main UI thread, then the timer won't tick.如果在不是主 UI 线程的线程中调用 Loopy() 方法,则计时器不会计时。 If you want to call this method from anywhere in the code then you need to check the InvokeRequired property.如果要从代码中的任何位置调用此方法,则需要检查InvokeRequired属性。 So your code should look like (assuming that the code is in a form):所以你的代码应该看起来像(假设代码是一种形式):

        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();
            }
        }

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
        {
            //
        }
    } 

}

Here's an Rx ticker that works:这是一个有效的Rx自动收报机:

Observable.Interval(TimeSpan.FromSeconds(1))
.Take(3)
.Subscribe(x=>Console.WriteLine("tick"));

Of course, you can subscribe something more useful in your program.当然,您可以在您的程序中订阅更有用的内容。

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
}

Check if your timer in properties is enabled.检查属性中的计时器是否已启用。 Mine was false and after setting to true it worked.我的是假的,设置为真后它起作用了。

If you use some delays smaller than the interval inside the timer, the system.timer will execute other thread and you have to deal with a double thread running at the same time.如果你使用一些小于定时器内部间隔的延迟,system.timer 将执行其他线程,你必须处理同时运行的双线程。 Apply an InvokeRequired to control the flow.应用 InvokeRequired 来控制流。

you may have started the timer from another thread , so try invoking it from the correct thread.您可能已从另一个线程启动计时器,因此请尝试从正确的线程调用它。 for example, instead of:例如,而不是:

timerX.start();

Use:利用:

Invoke((MethodInvoker)delegate { timerX.Start(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM