C# 如果在应用程序中使用 System.Timers.Timer 是否有必要处理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/475763/
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
Is it necessary to dispose System.Timers.Timer if you use one in your application?
提问by Roman Shumikhin
I am using System.Timers.Timer class in one of the classes in my application. I know that Timer class has Dispose method inherited from the parent Component class that implements IDisposable interface. Instances of the class below are created many times during my application lifecycle; each of them has an instance of Timer class that generates Elapsed events continuously during the class's lifecycle. Should I implement IDisposable interface in the class that uses Timer class to dispose the timer object? (I have seen code that doesn't do this at all). I am afraid that some unmanaged resources will not be freed if I use the class below like this:
我在我的应用程序中的一个类中使用 System.Timers.Timer 类。我知道 Timer 类具有从实现 IDisposable 接口的父 Component 类继承的 Dispose 方法。下面类的实例在我的应用程序生命周期中创建了很多次;他们每个人都有一个 Timer 类的实例,它在类的生命周期中连续生成 Elapsed 事件。我应该在使用 Timer 类来处理计时器对象的类中实现 IDisposable 接口吗?(我见过根本不这样做的代码)。如果我像这样使用下面的类,我担心一些非托管资源不会被释放:
SomeClass someClass = new SomeClass();
someClass.DoSomething();
someClass = null;
The class:
班上:
using System.Timers;
public class SomeClass
{
private Timer m_timer;
public SomeClass()
{
m_timer = new Timer();
m_timer.Interval = 1000;
m_timer.Elapsed += new ElapsedEventHandler(m_timer_Elapsed);
m_timer.AutoReset = false;
m_timer.Start();
}
public void DoSomething()
{
}
private void m_timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
//Do some task
}
catch (Exception ex)
{
//Ignore
}
finally
{
if (m_timer != null)
{
//Restart the timer
m_timer.Enabled = true;
}
}
}
}
采纳答案by flesh
Generally speaking you should always dispose of disposable resources. I certainly would be looking to in the case you outline above. If you implement IDisposable on the class that implements the timer you can then use the class in a using statement, meaning resources will be explicitly released when your class is disposed.
一般来说,您应该始终处理一次性资源。我当然会在你上面概述的情况下寻找。如果在实现计时器的类上实现 IDisposable,则可以在 using 语句中使用该类,这意味着在释放类时将显式释放资源。
回答by 1800 INFORMATION
I would guess that the timer object creates or uses a worker thread for the purposes of firing the timer events. The dispose call will free the thread and the resources associated with it. If that is the case, it would be a good idea to call dispose so you don't have unused threads hanging around too long.
我猜想计时器对象创建或使用工作线程是为了触发计时器事件。dispose 调用将释放线程和与之关联的资源。如果是这种情况,调用 dispose 将是一个好主意,这样您就不会让未使用的线程闲置太久。
回答by John
By implementing idisposable you will be able to tidy up any internal resources that also implement idisposable such as your timer.
通过实现 idisposable,您将能够整理任何也实现 idisposable 的内部资源,例如您的计时器。
In addition you would be able to change your calling code to use the using statment.
此外,您还可以更改调用代码以使用 using 语句。
using (SomeClass someClass = new SomeClass())
{
someClass.DoSomething();
}
回答by Rowland Shaw
The rule of thumb I use is to make anything that has an IDisposable object, IDisposable itself (and disposing the child objects only when Dispose is explicitly called)
我使用的经验法则是制作任何具有 IDisposable 对象的东西,即 IDisposable 本身(并且仅在显式调用 Dispose 时处理子对象)
There's a good discussion on IDisposable at Joe Duffy's blogalong with code samples which look very similar to those in my copy of the excellent Framework Design Guidelinesbook
在Joe Duffy 的博客上对 IDisposable以及代码示例进行了很好的讨论,这些示例与我在优秀的框架设计指南书中的副本非常相似
回答by GregC
I agree with Rowland.
我同意罗兰。
There is a rule in FxCop that finds classes containing Disposable objects, but not properly implementing IDisposable.
FxCop 中有一条规则可以查找包含 Disposable 对象但未正确实现 IDisposable 的类。
回答by uriDium
I see that you asked this question a year ago but let me throw in my 2 cents worth. Slightly less because of inflation :). Recently I discovered in our application that we weren't disposing of timers. We had a collection of objects and each object had a timer. When we removed the item from the collection we thought it should have been garbage collected. For some reason not so with timers. We had to call dispose on the object in the collection to get rid of the timer before the objects were actually garbage collected.
我看到你在一年前问过这个问题,但让我投入我的 2 美分。由于通货膨胀而略少:)。最近我在我们的应用程序中发现我们没有处理计时器。我们有一个对象集合,每个对象都有一个计时器。当我们从集合中删除项目时,我们认为它应该被垃圾收集。出于某种原因,定时器并非如此。我们必须在集合中的对象上调用 dispose 以在对象真正被垃圾收集之前摆脱计时器。
回答by Ian Ringrose
The timer has to be disposed, or it will keep fireing for some time after you have "finished" with it. However due to thread issues, it may still fire a short time after you have disposed it!
计时器必须被处理掉,否则它会在您“完成”它后继续触发一段时间。但是,由于线程问题,它可能在您处理后的短时间内仍会触发!