Timer not disposed when form is

久未见 提交于 2019-11-28 11:14:46

As I told you in my previous comment you should try:

private Form1_FormClosing(...)
{
    timer.Stop();
    timer.Tick -= new EventHandler(OnTimer);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
{
    timer.Dispose();
    timer = null;   
} 

This is good because you prevent timer to cycle again (in FormClosing) and you can check in other parts (non in this example because you're closing the form, but as example) if that object (timer) has been deleted before using it.
So in other parts you can do

if (timer != null) // Note: this is false if you just use timer.Dispose()
{
    ....
}

The only proper way to dispose disposable members of an IDisposable class is to do it inside its Dispose(bool disposing) method (check the MSDN article). In other words, you can open the autogenerated Form.Designer.cs file and put it inside the proper method.

On the other hand, if you add the Timer through VS Designer (instead of instantiating it yourself), it will get added to the components container:

// autogenerated inside Form.Designer.cs, InitializeComponent() method
this.timer = new System.Windows.Forms.Timer(this.components);

and then properly disposed when components member is disposed:

// autogenerated inside Form.Designer.cs, Dispose(bool disposing) method
if (disposing && (components != null))
{
    components.Dispose();
}

If you want to do this yourself, keep in mind that designer does not instantiate components if it doesn't think it's needed. So, components might be null in your case.

The simplest way to solve this: add the timer by dragging it from the toolbox, then start it inside the Form_Load handler.

 private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
 {        timer.Stop();    } 

I shouldn't put the this.Dispose(); in form closing event just stop timer.

The EventHandler is the persisting reference, remove the reference and Stop the timer on the Closing Event or as soon as its not required. If you want to check the Timer is disposed check it in the Closed Event

The simple Timer.Dispose() deletes timer resources, including stoping the timer from firing in the future.

However, it is possible that after Dispose() returns, there are callbacks that are either actively executing or sitting in the thread pool's work queue waiting to execute.

The second overload, Timer.Dispose(WaitHandle) will signal the passed in object once all callbacks for the timer have completed. This can be any WaitHandle, for instance a ManualResetEvent.

To simplify things, you can pass in WaitHandle.InvalidHandle and Timer.Dispose() will return only when all callbacks have completed. This avoids having to allocate a true event object and is usually what you want to do.

Since WaitHandle is abstract, you need to use a little hack, which is to create your own subclass.

class InvalidWaitHandle : WaitHandle {}
Timer tmr = new Timer(...);
tmr.Dispose(new InvalidWaitHandle);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!