I have a function that I'm using while(true) to repeatedly scan memory addresses of an application to detect change. I sleep the thread 1 second between iterations and this helps performance. However, is there a way to create a custom event handler to do away with the loops?
+2
A:
Have you looked into using a Timer with an Interval of 1000 (milliseconds)?
This also gives you a clean way out of your polling by simply destroying the timer, instead of having to check some condition and breaking out of your while loop.
Aaron Daniels
2010-03-31 02:20:08
Believe me I've thought about it. However, I was always taught to avoid timers.
Dremation
2010-03-31 02:21:10
The System.Threading.Timer is really tried and true. Once you have it setup correctly, there's no inherit issues with it. If you're not convinced, open up Reflector and you can see what the Timer is actually doing.
Aaron Daniels
2010-03-31 02:23:41
Thanks, I'll give that a go.
Dremation
2010-03-31 02:32:43
Pay special attention to this note on the timer in the MSDN dochttp://msdn.microsoft.com/en-us/library/system.threading.timer.aspxThe callback method executed by the timer should be reentrant, because it is called on ThreadPool threads. The callback can be executed simultaneously on two thread pool threads if the timer interval is less than the time required to execute the callback, or if all thread pool threads are in use and the callback is queued multiple times.
Conrad Frix
2010-03-31 02:47:28