■ 멀티미디어 타이머를 사용하는 방법을 보여준다.
▶ MultimediaTimerDelegate.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
namespace TestProject { /// <summary> /// 멀티미디어 타이머 대리자 /// </summary> /// <param name="id">ID</param> /// <param name="message">메시지</param> /// <param name="userContext">사용자 컨텍스트</param> /// <param name="reserved1">예약 1</param> /// <param name="reserved2">예약 2</param> public delegate void MultimediaTimerDelegate(uint id, uint message, ref uint userContext, uint reserved1, uint reserved2); } |
▶ MultimediaTimer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
using System; using System.ComponentModel; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 멀티미디어 타이머 /// </summary> public class MultimediaTimer : IDisposable { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 틱 이벤트 - Tick /// <summary> /// 틱 이벤트 /// </summary> public event EventHandler Tick; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 타이머 이벤트 설정하기 - TimeSetEvent(delay, resolution, callback, userContext, eventType) /// <summary> /// 타이머 이벤트 설정하기 /// </summary> /// <param name="delay">지연 시간 (단위 : ms)</param> /// <param name="resolution">해상도 (단위 : ms)</param> /// <param name="callback">콜백 함수</param> /// <param name="userContext">사용자 컨텍스트</param> /// <param name="eventType">이벤트 타입</param> /// <returns>타이머 ID</returns> /// <remarks> /// eventType : 0(1회성 이벤트), 1(정기 이벤트) /// </remarks> [DllImport("winmm", SetLastError = true, EntryPoint = "timeSetEvent")] private static extern uint TimeSetEvent(uint delay, uint resolution, MultimediaTimerDelegate callback, ref uint userContext, uint eventType); #endregion #region 타이머 이벤트 제거하기 - TimeKillEvent(timerID) /// <summary> /// 타이머 이벤트 제거하기 /// </summary> /// <param name="timerID">타이머 ID</param> [DllImport("winmm", SetLastError = true, EntryPoint = "timeKillEvent")] private static extern void TimeKillEvent(uint timerID); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 리소스 해제 여부 /// </summary> private bool disposed = false; /// <summary> /// 주기 /// </summary> private int interval; /// <summary> /// 해상도 (단위 : ms) /// </summary> private int resolution; /// <summary> /// 타이머 ID /// </summary> private uint timerID; /// <summary> /// 멀티미디어 타이머 대리자 /// </summary> private readonly MultimediaTimerDelegate multimediaTimerDelegate; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 주기 - Interval /// <summary> /// 주기 /// </summary> public int Interval { get { return this.interval; } set { if(this.disposed) { throw new ObjectDisposedException("MultimediaTimer"); } if(value < 0) { throw new ArgumentOutOfRangeException("Interval"); } this.interval = value; if(Resolution > Interval) { Resolution = value; } } } #endregion #region 해상도 - Resolution /// <summary> /// 해상도 /// </summary> public int Resolution { get { return this.resolution; } set { if(this.disposed) { throw new ObjectDisposedException("MultimediaTimer"); } if(value < 0) { throw new ArgumentOutOfRangeException("Resolution"); } this.resolution = value; } } #endregion #region 실행 여부 - IsRunning /// <summary> /// 실행 여부 /// </summary> public bool IsRunning { get { return this.timerID != 0; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MultimediaTimer() /// <summary> /// 생성자 /// </summary> public MultimediaTimer() { this.multimediaTimerDelegate = new MultimediaTimerDelegate(ProcessMultimediaTimer); Resolution = 5; Interval = 10; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Destructor #region 소멸자 - ~MultimediaTimer() /// <summary> /// 소멸자 /// </summary> ~MultimediaTimer() { Dispose(false); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 시작하기 - Start() /// <summary> /// 시작하기 /// </summary> public void Start() { if(this.disposed) { throw new ObjectDisposedException("MultimediaTimer"); } if(IsRunning) { return; } uint userContext = 0; this.timerID = TimeSetEvent((uint)Interval, (uint)Resolution, multimediaTimerDelegate, ref userContext, 1); if(this.timerID == 0) { int error = Marshal.GetLastWin32Error(); throw new Win32Exception(error); } } #endregion #region 중단하기 - Stop() /// <summary> /// 중단하기 /// </summary> public void Stop() { if(this.disposed) { throw new ObjectDisposedException("MultimediaTimer"); } if(!IsRunning) { return; } StopInternal(); } #endregion #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { Dispose(true); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 멀티미디어 타이머 처리하기 - ProcessMultimediaTimer(id, message, userContext, reserved1, reserved2) /// <summary> /// 멀티미디어 타이머 처리하기 /// </summary> /// <param name="id">ID</param> /// <param name="message">메시지</param> /// <param name="userContext">사용자 컨텍스트</param> /// <param name="reserved1">예약 1</param> /// <param name="reserved2">예약 2</param> private void ProcessMultimediaTimer(uint id, uint message, ref uint userContext, uint reserved1, uint reserved2) { Tick?.Invoke(this, EventArgs.Empty); } #endregion #region 중단하기 (내부용) - StopInternal() /// <summary> /// 중단하기 (내부용) /// </summary> private void StopInternal() { TimeKillEvent(this.timerID); this.timerID = 0; } #endregion #region 리소스 해제하기 - Dispose(disposing) /// <summary> /// 리소스 해제하기 /// </summary> /// <param name="disposing">리소스 해제 여부</param> private void Dispose(bool disposing) { if(this.disposed) { return; } this.disposed = true; if(IsRunning) { StopInternal(); } if(disposing) { Tick = null; GC.SuppressFinalize(this); } } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
using System; using System.Diagnostics; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 스톱워치 /// </summary> private Stopwatch stopwatch = new Stopwatch(); /// <summary> /// 멀티미디어 타이머 /// </summary> private MultimediaTimer multimediaTimer; /// <summary> /// 표시 타이머 /// </summary> private Timer displayTimer; /// <summary> /// 현재 경과 시간 (단위 : 밀리초) /// </summary> private double currentElapsedTime; /// <summary> /// 이전 시간 (단위 : 밀리초) /// </summary> private double previousTime; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.multimediaTimer = new MultimediaTimer(); this.multimediaTimer.Interval = 1; this.multimediaTimer.Resolution = 0; this.displayTimer = new Timer(); this.displayTimer.Interval = 30; FormClosing += Form_FormClosing; this.multimediaTimer.Tick += multimediaTimer_Tick; this.displayTimer.Tick += displayTimer_Tick; this.multimediaTimer.Start(); this.displayTimer.Start(); this.stopwatch.Start(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폼을 닫을 경우 처리하기 - Form_FormClosing(sender, e) /// <summary> /// 폼을 닫을 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_FormClosing(object sender, FormClosingEventArgs e) { this.displayTimer.Stop(); this.multimediaTimer.Stop(); } #endregion #region 멀티미디어 타이머 틱 처리하기 - multimediaTimer_Tick(sender, e) /// <summary> /// 멀티미디어 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void multimediaTimer_Tick(object sender, EventArgs e) { this.currentElapsedTime = this.stopwatch.ElapsedMilliseconds - this.previousTime; Console.WriteLine($"멀티미디어 타이머 : [{DateTime.UtcNow:o}] {currentElapsedTime}"); this.previousTime = this.stopwatch.ElapsedMilliseconds ; } #endregion #region 표시 타이머 틱 처리하기 - displayTimer_Tick(sender, e) /// <summary> /// 표시 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void displayTimer_Tick(object sender, EventArgs e) { this.currentElaspesTimeValueLabel.Text = this.currentElapsedTime.ToString(); } #endregion } } |