[System.Windows.Forms.Timer], [System.Timers.Timer], [System.Threading.Timer]
C#, .NET 2011. 6. 17. 19:42<System.Windows.Forms.Timer>
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer(v=VS.90).aspx
A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.
<System.Timers.Timer>
http://msdn.microsoft.com/en-us/library/system.timers.timer(v=VS.90).aspx
The server-based Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time. For more information on server-based timers, see Introduction to Server-Based Timers.
<System.Threading.Timer>
http://msdn.microsoft.com/en-us/library/system.threading.timer(v=VS.90).aspx
Use a TimerCallback delegate to specify the method you want the Timer to execute. The timer delegate is specified when the timer is constructed, and cannot be changed. The method does not execute on the thread that created the timer; it executes on a ThreadPoolthread supplied by the system.
위 설명대로 이녀석은 thread pool에 있는 thread들 사이를 이리저리 옮겨다닌다.
pool에서 대기중인 thread를 하나 골라 잡아서 Elapsed event를 발생시키고 event handler를 실행한다.
그런데 SynchronizingObject 속성이 null 이 아니면 이 속성이 가리키는 객체가 생성된 thread 에서만 event handler를 실행한다.
그런데 그 thread가 pool에서 대기중이 아니거나 한다면.. 그냥 event가 발생되지 않는다.
3개의 Timer 비교
http://msdn.microsoft.com/en-us/library/tb9yt5e6(v=VS.90).aspx
There are three timer controls in Visual Studio and the .NET Framework:
A server-based timer, which you can add to the Toolbox
A Windows-based timer, which is always in the Toolbox
A thread timer, which is available programmatically
The Windows-based timer is optimized for use in Windows Forms applications. The server-based timer is an update of the traditional timer that has been optimized to run in a server environment. The thread timer is a simple, lightweight timer that uses callback methods instead of events and is served by thread-pool threads.
그 외 참고
http://csharptips.wordpress.com/2010/08/24/thread-timer/
셋다 Dispose() 를 잊지 말자.
그리고 Dispose() 후에 모든 reference를 null 로 설정하여 GC에 의해 수거되도록 하자
근데 System.Threading.Timer 뭔가 이상함.. 여러개를 계속 돌리면 왜 죽지.. 시작/정지 함수도 없고 ㅠㅠ
'C#, .NET' 카테고리의 다른 글
직접 구현한 MinHeap (0) | 2011.06.17 |
---|---|
value type에 관한 진실 (0) | 2011.06.17 |
listbox auto scroll (0) | 2011.06.17 |
network 상태 체크하기 (0) | 2011.06.17 |
log4net (0) | 2011.06.17 |