Compact Framework: Timer.Dispose is not blocking
UPDATE: The Timer is a System.Threading.Timer.
We have a UI control that is updated on Timer Tick. On dispose of our control we call timer.Dispose(). We are running into occasional ObjectDisposedOExceptions when the timer fires after the UI control has been invoked.
I see this in the msdn docs:
"Callbacks can occur after the Dispose() method overload has been called, because the timer queues callbacks for execution by th开发者_如何学Cread pool threads. You can use the Dispose(WaitHandle) method overload to wait until all callbacks have completed."
However, the Compact Framework does NOT have access to that overloaded method.
What is the best workaround for this situation? Is there a way to make our Dispose method block until the timer has been fully disposed? Other suggestions?
Thank you!
I'm not sure it would be a good idea to block on Dispose. There are a number of things that could go wrong that could leave things in an unclean state.
I would instead check for the control being null on your timer callback. Alternatively you could put your timer callback inside of a try block and catch the ObjectDisposedException but this will consume a lot more resources (exception throwing/catching is very expensive.)
This should only happen until the threads in the threadpool are exhausted and it sounds like there's no real problem if the timer signals a couple times but doesn't do any work because the control is null.
精彩评论