I am trying to understand why a Windows.Forms.Timer
is not disposed when the form
that created it is. I have this simple form:
public partial class Form1 : Form {
private System.Windows.Forms.Timer timer;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(OnTimer);
timer.Enabled = true;
}
private void OnTimer(Object source, EventArgs e) {
Debug.WriteLine("OnTimer entered");
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
this.Dispose();
}
}
When I close it, this.Dispose
is called but the timer firing event continues to be called. I thought that the Dispose
was freeing all objects owned by the disposed object. Is that untrue? Does Timer
have a specific behavior?
For now, I found that the way to dispose of the timer is to do timer.Tick -= OnTimer;
- I call it then in the Form1_FormClosed
event. Is it the good solution or should I do otherwise?
EDIT
Or is it simply better to do:
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
timer.Dispose();
this.Dispose();
}
?
I shouldn't put the this.Dispose(); in form closing event just stop timer.
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 anyWaitHandle
, for instance aManualResetEvent
.To simplify things, you can pass in
WaitHandle.InvalidHandle
andTimer.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.As I told you in my previous comment you should try:
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
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 only proper way to dispose disposable members of an
IDisposable
class is to do it inside itsDispose(bool disposing)
method (check the MSDN article). In other words, you can open the autogeneratedForm.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 thecomponents
container:and then properly disposed when
components
member is disposed: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 benull
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.