简体   繁体   English

创建新计时器时,旧计时器会怎样?

[英]When creating a new timer, what will happen to the old?

I have inherit some old code and it looks to me that there is an problem with some part(s) of it. 我继承了一些旧代码,在我看来它的某些部分存在问题。

The program uses TCP/IP to communicate with another program and the protocol is simple. 该程序使用TCP / IP与另一个程序进行通信,协议很简单。 Send a command Telegram and wait for a response telegram. 发送命令电报并等待响应电报。

Here is the part that's I think is problematic. 我认为这是有问题的部分。

public System.Timers.Timer retransmitTimer;

public TelegramBase SendAndWait(TelegramBase telegram)
{
    CurrentTelegram = telegram;

    retransmitTimer = new Timer(RetransmitInterval);
    retransmitTimer.Elapsed += retransmitTimer_Elapsed;

    //Send telegram
    Send(telegram);

    //Start timer
    retransmitTimer.Start();

    //Wait for response
    var response = WaitForResponse(telegram as StandardTelegram);

    //stop timer
    retransmitTimer.Stop();

    return response;
}

The method SendAndWait is called every time a command telegram is sent. 每次发送命令电报时都会调用方法SendAndWait。

My issue is the creation of the timer 我的问题是计时器的创建

    retransmitTimer = new Timer(RetransmitInterval);
    retransmitTimer.Elapsed += retransmitTimer_Elapsed;

This will create a new timer, but the current one is never disposed so it will keep on running? 这将创建一个新计时器,但是当前计时器永远不会被丢弃,因此它将继续运行吗? Best scenario it will be stopped. 最好的情况将被停止。

What is better? 什么是更好的?

  1. Move the creation of the timer to a method that is only called once? 将计时器的创建移至仅调用一次的方法?
  2. Dispose of the current timer and then create a new one in SendAndWait? 处置当前计时器,然后在SendAndWait中创建一个新计时器?

I would wrap the timer in a using statement: 我将计时器包装在using语句中:

public TelegramBase SendAndWait(TelegramBase telegram)
{
    CurrentTelegram = telegram;

    using (Timer retransmitTimer = new Timer(RetransmitInterval))
    {
        retransmitTimer.Elapsed += retransmitTimer_Elapsed;

        //Send telegram
        Send(telegram);

        //Start timer
        retransmitTimer.Start();

        //Wait for response
        var response = WaitForResponse(telegram as StandardTelegram);

        //stop timer
        retransmitTimer.Stop();
    }

    return response;
}

From the description of System.Timers.Timer , it seems to be quite heavy object, so initializing it with every message may have pretty large overhead. System.Timers.Timer的描述来看,它似乎是很沉重的对象,因此,每条消息初始化它都可能会产生很大的开销。 If you feel like you are struggling with performance, this would be the place to look. 如果您觉得自己在性能方面苦苦挣扎,那么这里就是您要找的地方。 You could do some microbenchmarks to time the execution time of the timer constructor compared to the rest of the method. 与方法的其余部分相比,您可以做一些微基准测试来计时计时器构造函数的执行时间。

In your current implementation, the retransmitTimer should be local variable contained in a using statement, as you can see in the other answer. 在当前的实现中, retransmitTimer应该是using语句中包含的局部变量,如您在其他答案中所见。

If you decide to use one timer per class, then instead of calling the Timer constructor with RetransmittInterval , you can use the Interval property of existing Timer instance to set the interval. 如果决定每个类使用一个计时器,则可以使用现有Timer实例的Interval属性来设置时间间隔,而不必使用RetransmittInterval调用Timer构造函数。 With this implementation you should implement IDisposable on the class that contains the SendAndWait method, and dispose the timer in the Dispose method of your class. 使用此实现,您应该在包含SendAndWait方法的类上实现IDisposable ,并将计时器放置在您的类的Dispose方法中。

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

相关问题 调用对象的新实例会怎样? - What happen when new Instance of an object call? 在旧的视频驱动程序中显示32BPP PNG(ony 256色)时会发生什么 - what will happen when 32BPP PNG is displayed in the old video driver (ony 256 colors) C#旧计时器与新计时器重叠 - C# Old timer overlap the new 在不使用new关键字的情况下构建结构时会发生什么? - What does happen when you make a struct without using the new keyword? 在 ASP.NET CORE MVC 中创建新记录时如何将日期从新日期更改为旧日期 - How to Change Date from new to old date when creating new record in ASP.NET CORE MVC 设置DisplayMember后,DataGridViewComboBoxColumn会发生什么情况? - What happen to DataGridViewComboBoxColumn when DisplayMember is set? 在计时器刻度性能/ GC上创建新实例? - Creating a new instance on timer tick performance/GC? 在Excel中双击单元格会发生什么? - What will happen when a cell is double clicked in excel? 按下按键时,创建自定义弹出键盘没有任何反应 - Creating custom pop up keyboard nothing happen when keys are press 当我在我的计时器中使用xlApp.CutCopyMode时,我的应用程序不会关闭。 为什么会这样? - When I use xlApp.CutCopyMode in my timer my application does not close. Why does this happen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM