static void AnimateImages50ms() { for (int i = 0; i < images.Count; i++) { ImageInfo image = (ImageInfo)images[i]; // Frame delay is measured in 1/100ths of a second. This thread // sleeps for 50 ms = 5/100ths of a second between frame updates, // so we increase the frame delay count 5/100ths of a second // at a time. // image.FrameTimer += 5; if (image.FrameTimer >= image.FrameDelay(image.Frame)) { image.FrameTimer = 0; if (image.Frame + 1 < image.FrameCount) { image.Frame++; } else { image.Frame = 0; } } } }
/// <summary> /// Worker thread procedure which implements the main animation loop. /// NOTE: This is the ONLY code the worker thread executes, keeping it in one method helps better understand /// any synchronization issues. /// WARNING: Also, this is the only place where ImageInfo objects (not the contained image object) are modified, /// so no access synchronization is required to modify them. /// </summary> private static void AnimateImages50ms() { Debug.Assert(s_imageInfoList != null, "Null images list"); while (true) { // Acquire reader-lock to access imageInfoList, elemens in the list can be modified w/o needing a writer-lock. // Observe that we don't need to check if the thread is waiting or a writer lock here since the thread this // method runs in never acquires a writer lock. s_rwImgListLock.AcquireReaderLock(Timeout.Infinite); try { for (int i = 0; i < s_imageInfoList.Count; i++) { ImageInfo imageInfo = s_imageInfoList[i]; // Frame delay is measured in 1/100ths of a second. This thread // sleeps for 50 ms = 5/100ths of a second between frame updates, // so we increase the frame delay count 5/100ths of a second // at a time. // imageInfo.FrameTimer += 5; if (imageInfo.FrameTimer >= imageInfo.FrameDelay(imageInfo.Frame)) { imageInfo.FrameTimer = 0; if (imageInfo.Frame + 1 < imageInfo.FrameCount) { imageInfo.Frame++; } else { imageInfo.Frame = 0; } if (imageInfo.FrameDirty) { s_anyFrameDirty = true; } } } } finally { s_rwImgListLock.ReleaseReaderLock(); } Thread.Sleep(50); } }
private static void AnimateImages50ms() { while (true) { rwImgListLock.AcquireReaderLock(-1); try { for (int i = 0; i < imageInfoList.Count; i++) { ImageInfo info = imageInfoList[i]; info.FrameTimer += 5; if (info.FrameTimer >= info.FrameDelay(info.Frame)) { info.FrameTimer = 0; if ((info.Frame + 1) < info.FrameCount) { info.Frame++; } else { info.Frame = 0; } if (info.FrameDirty) { anyFrameDirty = true; } } } } finally { rwImgListLock.ReleaseReaderLock(); } Thread.Sleep(50); } }