|
-
July 8th, 2011, 01:04 PM
#1
Troubleshooting Timers with VS 2010 Express
Is there a way to see all of the active timers in an application in VS 2010? My application sets many timers, and I want to see if they are getting disposed and created when they are supposed to.
-
July 9th, 2011, 06:52 PM
#2
Re: Troubleshooting Timers with VS 2010 Express
One way to know which timers are active is for you to track them yourself. Especially because the timers provided by the framework don't have a way of directly conveying this information.
For example, you could simply add them to a list; you can call it _activeTimers. When you stop a timer, remove it. This list will just hold references to the same timers you have stored elsewhere - it will not be a list of copies. It, of course, must be available to the code that needs to access the active timers. Just make sure that each time you start or stop a timer, you update the list too.
Another way is, if you're using the derived class from your other thread, to add this functionality to that class. A simple bool property that returns the active/inactive status of the timer will do. However, two things of importance: (1) this might turn out to be a bit tricky, and (2) with this approach, you would have to check each timer to see if it's active or not, while with the first one you don't need to do any additional processing - all the active timers will simply be in the list.
As for disposing, you can also make sure to call Dispose() on the timer whenever you remove it from the active timers list, so you can just check if the list is empty in the end. Other than that, no object that is subject to garbage collection will be collected for as long as there exist any references to it. A reference is released if it's explicitly set to null, or, for example, if it was local, and went out of scope.
However, most people aren't aware that events are references to the objects that contain the assigned event handlers. So, these should be unhooked when no longer needed.
-
July 9th, 2011, 10:59 PM
#3
Re: Troubleshooting Timers with VS 2010 Express
That's pretty much what I thought, and I do already add my timers to a Dictionary. I do dispose of the timer and then remove it from the Dictionary at the same time, but in the interest of thoroughness, I just wanted to see if there was an easy way to see all running timers. I do like your idea of adding that functionality to my timer class that I am writing. I hadn't done that.
-
July 10th, 2011, 08:42 AM
#4
Re: Troubleshooting Timers with VS 2010 Express
Have you ever thought to redesign your app so it doesn't use timers?
You know, timers aren't all that reliable because they are dependent on the action of the user. They reside in the same message queue as the rest of the user messages and user actions can interrupt the processing of those messages. In addition, excessive timer usage can make your app appear sluggish and non-responsive to the user.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|