![]() |
.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 |
In the form's Close event, call the timer's Dispose function:
:
Timer1.Dispose(); |
Quote:
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 |
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.
|
Quote:
-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