是否有必要处置System.Timers.Timer的,如果你使用一个在你的应用程序?你的、如果你、有必要、应用程序

2023-09-02 11:53:59 作者:会为我吗

我的班我的应用程序中的一个使用System.Timers.Timer的类。 我知道Timer类有Dispose方法从实现IDisposable接口父组件类继承。 下面是在我的应用程序生命周期创造了许多倍的类的实例;他们每个人都有类的生命周期中不断生成的事件经过的Timer类的一个实例。 我应该落实在使用Timer类处置计时器对象的类IDisposable接口? (我见过code,不这样做的话)。 恐怕一些非托管资源不会被释放,如果我使用的类下面是这样的:

  SomeClass的SomeClass的=新SomeClass的();
someClass.DoSomething();
SomeClass的= NULL;
 

类:

 使用System.Timers;

公共类SomeClass的
{
    私人定时器m_timer;

    公共SomeClass的()
    {
        m_timer =新的Timer();
        m_timer.Interval = 1000;
        m_timer.Elapsed + =新ElapsedEventHandler(m_timer_Elapsed);
        m_timer.AutoReset = FALSE;
        m_timer.Start();
    }

    公共无效DoSomething的()
    {

    }

    私人无效m_timer_Elapsed(对象发件人,ElapsedEventArgs E)
    {
        尝试
        {
            //做一些任务
        }
        赶上(例外前)
        {
            //忽略
        }
        最后
        {
            如果(m_timer!= NULL)
            {
                //重新启动定时器
                m_timer.Enabled = TRUE;
            }
        }
    }
}
 

解决方案

一般来说,你应该总是处置可支配的资源。我当然会期待的情况下,你上面的概述。如果您在实现那么你可以使用这个类在using语句,意味着资源的计时器,当你的类配置将被明确释放类实现IDisposable。

为什么 System.Timers.Timer 更改间隔时间后的第一次触发时间是设定时间的三倍 ...

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:

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

解决方案

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.