Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   .NET Timer Form closing issue (http://www.programmingforums.org/showthread.php?t=7070)

MBirchmeier Nov 18th, 2005 4:21 PM

.NET Timer Form closing issue
 
This question pertains specifically to the .NET Compact framework version 1.1.

The problem is using Timers with the Form.ShowDialog() and Form.Close() command.

When a timer is created on a form with the Form.Show() command it is attached to the form, so that the form will stop and (supposedly) close the timer when the form is destroyed, however with the ShowDialog command is used.

However when using the ShowDialog command the timer is attached differently to the form so that it can keep running after the form has been closed, and in some cases even destroyed. (note: We can't use Show instead of ShowDialog() for various other reasons, notably our dynamic creations of forms from a database tends to issues with Form.Show())

This causes two problems.

First we'll occasionally have a timer run, that because the form underneath it has been destroyed, that will cause various unhandled null reference exceptions and the like.

Secondly : Even if the timer is not running, it appears that it is never getting destroyed, since that is normally handled in the form destruction code. (Or the code will keep running, without causing an exception) causing a small possibly undetectable memory leak. (This code is designed to run for months if not years at a time, a small memory leak can be substancial, we've ran tests of several weeks at a time without problem, but a leak this small we might not be able to detect over such a small period of time)

Is there a way to force the timers destruction with the destruction or closing of the form? Additionally is there a way to automate this process to prevent from having to individually evaluate each form.

Thanks,
-MBirchmeier

Ooble Nov 18th, 2005 6:35 PM

In the form's Close event, call the timer's Dispose function:
:

Timer1.Dispose();

MBirchmeier Nov 21st, 2005 9:19 AM

Quote:

Originally Posted by Ooble
In the form's Close event, call the timer's Dispose function:
:

Timer1.Dispose();

This actually doesn't quite work, there's situations where the close event occurs before all of the timers are fully initialized. (.NET is an odd language timingwise)

It was suggested elsewhere that the timer class be overrided and have a method that watches the form beneath it and destroys when in destroys.
The other option would be to add a Form.Closed() event and close it there.

-MBirchmeier

OpenLoop Nov 21st, 2005 10:50 AM

You can modify the overloaded Dispose() method to explicitly destroy the timer. As you might know, the Dispose() method is called when the form is closed.

MBirchmeier Nov 21st, 2005 11:00 AM

Quote:

Originally Posted by OpenLoop
You can modify the overloaded Dispose() method to explicitly destroy the timer. As you might know, the Dispose() method is called when the form is closed.

This was discussed as a solution, but it's one we're trying to avoid. It would require code changes in quite a few places (2 or 3 dozen... the project design wasn't exactly thought out early on). So we're trying to make functional changes in as few places as possible.

-MBirchmeier


All times are GMT -5. The time now is 8:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC