[英]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? 什么是更好的?
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.