Windows Develop Bookmark and Share   
 index > Windows Forms General > Timer not disposed
 

Timer not disposed

I have a problem with a timer continuing to fire it's tick event after my form has closed. Not only that, but everytime I reopen the form, I get a new timer instance, with a new event, which eventually results in the application becoming unuseable.

I have tried disabling the timer in the FormClosing event, and even disposing the timer object, as well as removing its event handle, but still cannot stop it.

My form is a simple log viewer that runs as a child control within a master form container. It is written in C# and is only requiring a timer event to refresh the form automatically. My form closing code looks like this:

private void LogViewer_FormClosing(object sender, FormClosingEventArgs e)

{

this.tmRefresh.Stop( );

this.tmRefresh.Tick -= new System.EventHandler(this.tmRefresh_Tick);

this.tmRefresh.Dispose( );

this.Visible = false;

}

but it does not stop the timer tick event firing!

Does anyonehave any suggestions?

RossL321  Monday, June 25, 2007 6:26 AM
That's not what I had in mind, I meant accidentally creating a new instance. Another possible failure mode is not adding the timer to the form with the designer but creating it yourself in the form constructor or Load event handler in code. Or displaying the form with ShowDialog() and not calling Dispose() explicitly afterwards.
nobugz  Tuesday, June 26, 2007 2:11 AM
You are probably creating a new instance of the form by accident, it is easy to do and is not obvious when you never called Show(). Set a breakpoint on the form constructor to diagnose the problem.
nobugz  Monday, June 25, 2007 9:05 AM

Thankyou for your reply, and yes you are right, I am creating a new instance of the child form. It is not unreasonable in the context of the application to have multiple log viewer forms open displaying different job logs.

What I am having difficulty with is the removal of the timer event once a log viewer form is closed. When I set a breakpoint in the tick events code it is still being invoked after the form has closed. This tells me thatI haven't successfully deleted the event handler and that the form hasn't been disposed of after it was closed, but I am none the wiser as to why. I is suggesting to me that the timer control is not actually a child control of the form, even though it is attached to the form and instantiated by the form during initialisation.

Would it be preferable to set the timer control on the main form and add the log viewer form events to that control, removing them in the usual way when the log viewer forms close?

RossL321  Tuesday, June 26, 2007 12:54 AM
have you tried to do:

tmRefresh.Enabled = false;
tmRefresh.dispose();


ScubaSteve20001  Tuesday, June 26, 2007 1:00 AM
That's not what I had in mind, I meant accidentally creating a new instance. Another possible failure mode is not adding the timer to the form with the designer but creating it yourself in the form constructor or Load event handler in code. Or displaying the form with ShowDialog() and not calling Dispose() explicitly afterwards.
nobugz  Tuesday, June 26, 2007 2:11 AM

You can use google to search for other answers

Custom Search

More Threads

• How to create an extended form base class?
• ShowCHeckboxes in windows.forms.treeview
• windows secure shell
• Creating and naming new controls at runtime
• Showing Icons In A DataGridViewImageColumn
• how to return values from form?
• Is it running from Command Line?
• setting label value in formview verses outside the formview control
• Filling ComboBox From Array
• how to pick a word by pointing cursor