简体   繁体   English

如何停止在C#类中运行的计时器

[英]How to stop a timer running inside a class C#

I have a timer running inside a class. 我有一个在类中运行的计时器。 I have two push buttons. 我有两个按钮。 Push button 1 is to start the timer(which is inside a class) Push button 2 is to stop the timer forcefully. 按钮1用于启动计时器(位于班级内)按钮2用于强制停止计时器。 Timer runs smoothly when click on push button 1 and perform calculations. 单击按钮1并执行计算时,计时器运行平稳。 But when I press Push button 2 the timer does not stopped forcefully and throws an exception 但是当我按下按钮2时,计时器并没有强行停止并引发异常

An unhandled exception of type 'System.NullReferenceException' occurred ". 类型为'System.NullReferenceException'的未处理异常发生。

Note:if I use public static System.Timers.Timer theTimer1; 注意:如果我使用公共静态System.Timers.Timer theTimer1, then timer could be stopped with push button 2.But I dont want to use static and when if condition will be true then it stopped automatically but stop method is not working with push button. 然后可以使用按钮2停止计时器。但是我不想使用static,并且当条件为true时,它将自动停止,但是stop方法不适用于按钮。 How can I stop this timer with push button. 如何使用按钮停止计时器。 Thanks. 谢谢。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();             
    } 

    Test acc = new Test();

    private void button1_Click(object sender, EventArgs e) 
    {
        acc.SerialPortEvent()
    }

    private void button2_Click(object sender, EventArgs e) 
    {
        acc.stop();
    }
}

class Test
{
    public System.Timers.Timer theTimer1;

    public void SerialPortEvent()
    {
        theTimer1 = new System.Timers.Timer(10000);
        theTimer1.Elapsed += timer_Tick;
        theTimer1.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        //Perform some calculations here.
        if (something like counter)
        {
            stop();
        }
    }

    public void stop()
    {
        theTimer1.Stop();
        theTimer1.Dispose();
    }
}

Initialize the timer in the constructor of Test : Test的构造函数中初始化计时器:

class Test
{
   public Test()
   {
       theTimer1 = new System.Timers.Timer(10000);
   }

Your code initializes the timer only when SerialPortEvent() is called. 您的代码仅在调用SerialPortEvent()时初始化计时器。

This is not called until you press the Start button, hence you get a null reference exception when you press Stop without pressing Start first. 在按下“ Start按钮之前,不会调用此方法,因此在按“ Stop而不先按“ Start Stop情况下会出现空引用异常。

Furthermore your stop() function disposes the timer. 此外,您的stop()函数可配置计时器。 This might be an issue when you try to stop the timer after it was disposed. 当您在处置计时器后尝试停止计时器时,这可能是个问题。

It is most likely that you're trying to stop your Timer from a different thread 您很可能试图从其他线程停止Timer

So let's assume that you are in thread 1 and you create a reference to your timer that is instantiated in thread 2. After some time thread 1 has ended and you are in thread 3 where you're trying to reach that reference from thread 1. That's why you got null reference exception. 因此,假设您在线程1中,并创建了对在线程2中实例化的计时器的引用。一段时间之后,线程1结束,您在线程3中,您正尝试从线程1到达该引用。这就是为什么您会得到null引用异常。

You should either use static, volatile or Task and Task Cancellation Token. 您应该使用静态,易失性或“任务和任务取消令牌”。

public volatile System.Timers.Timer theTimer1;

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

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