ImageAnimator.StopAnimate() 似乎并没有真正做任何事情

本文关键字:任何事 并没有 StopAnimate ImageAnimator | 更新日期: 2023-09-27 17:56:25

我第一次使用 ImageAnimator 对象,发现对 StopAnimate() 的调用似乎并没有真正阻止底层线程运行并调用 FrameChanged 处理程序。

我创建了一个测试来说明这一点,我想知道是否有任何具有 ImageAnimator 经验的人可以稍微了解一下 StopAnimate() 方法实际上应该做什么。 MSDN说它应该停止动画,但它没有。

意识到我可以使用布尔值来控制何时处理更新,但他是一种解决方法,并使(如果有效)StopAnimate() 方法变得多余。

下面是一些测试代码,无需外部资源:

using System;
using System.IO;
using System.Threading;
using System.Drawing;
namespace ImageAnimatorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            AnimateGif();
        }
        private static void AnimateGif()
        {
            Image testImage = null;
            using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(GifData)))
            {
                testImage = Image.FromStream(stream);
            }
            ImageAnimator.Animate(testImage, delegate(object o, EventArgs args)
            {
                //  Each time the frame is updated this will fire
                Console.WriteLine("Tick");
            });
            Thread.Sleep(750);
            //  The following code should stop the "animation" for the supplied image which I would
            //  THINK would stop the frame changed callback from firing...
            Console.WriteLine("Stop requested, should not see more '"Ticks'"...");
            ImageAnimator.StopAnimate(testImage, delegate(object o, EventArgs args)
            {
                Console.WriteLine("Stopped");
            });
            Console.ReadLine();
        }
        private static string GifData =
            "R0lGODlhEAAQAPQAAP///wAAAPDw8IqKiuDg4EZGRnp6egAAAFhYWCQkJKysrL6+vhQUFJycnAQEBDY2Nmh" +
            "oaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAw" +
            "EAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAAFdyAgAgIJIeWoA" +
            "kRCCMdBkKtIHIngyMKsErPBYbADpkSCwhDmQCBethRB6Vj4kFCkQPG4IlWDgrNRIwnO4UKBXDufzQvDMaoS" +
            "DBgFb886MiQadgNABAokfCwzBA8LCg0Egl8jAggGAA1kBIA1BAYzlyILczULC2UhACH5BAkKAAAALAAAAAA" +
            "QABAAAAV2ICACAmlAZTmOREEIyUEQjLKKxPHADhEvqxlgcGgkGI1DYSVAIAWMx+lwSKkICJ0QsHi9RgKBwn" +
            "VTiRQQgwF4I4UFDQQEwi6/3YSGWRRmjhEETAJfIgMFCnAKM0KDV4EEEAQLiF18TAYNXDaSe3x6mjidN1s3I" +
            "QAh+QQJCgAAACwAAAAAEAAQAAAFeCAgAgLZDGU5jgRECEUiCI+yioSDwDJyLKsXoHFQxBSHAoAAFBhqtMJg" +
            "8DgQBgfrEsJAEAg4YhZIEiwgKtHiMBgtpg3wbUZXGO7kOb1MUKRFMysCChAoggJCIg0GC2aNe4gqQldfL4l" +
            "/Ag1AXySJgn5LcoE3QXI3IQAh+QQJCgAAACwAAAAAEAAQAAAFdiAgAgLZNGU5joQhCEjxIssqEo8bC9BRjy" +
            "9Ag7GILQ4QEoE0gBAEBcOpcBA0DoxSK/e8LRIHn+i1cK0IyKdg0VAoljYIg+GgnRrwVS/8IAkICyosBIQpB" +
            "AMoKy9dImxPhS+GKkFrkX+TigtLlIyKXUF+NjagNiEAIfkECQoAAAAsAAAAABAAEAAABWwgIAICaRhlOY4E" +
            "IgjH8R7LKhKHGwsMvb4AAy3WODBIBBKCsYA9TjuhDNDKEVSERezQEL0WrhXucRUQGuik7bFlngzqVW9LMl9" +
            "XWvLdjFaJtDFqZ1cEZUB0dUgvL3dgP4WJZn4jkomWNpSTIyEAIfkECQoAAAAsAAAAABAAEAAABX4gIAICuS" +
            "xlOY6CIgiD8RrEKgqGOwxwUrMlAoSwIzAGpJpgoSDAGifDY5kopBYDlEpAQBwevxfBtRIUGi8xwWkDNBCIw" +
            "mC9Vq0aiQQDQuK+VgQPDXV9hCJjBwcFYU5pLwwHXQcMKSmNLQcIAExlbH8JBwttaX0ABAcNbWVbKyEAIfkE" +
            "CQoAAAAsAAAAABAAEAAABXkgIAICSRBlOY7CIghN8zbEKsKoIjdFzZaEgUBHKChMJtRwcWpAWoWnifm6ESA" +
            "MhO8lQK0EEAV3rFopIBCEcGwDKAqPh4HUrY4ICHH1dSoTFgcHUiZjBhAJB2AHDykpKAwHAwdzf19KkASIPl" +
            "9cDgcnDkdtNwiMJCshACH5BAkKAAAALAAAAAAQABAAAAV3ICACAkkQZTmOAiosiyAoxCq+KPxCNVsSMRgBs" +
            "iClWrLTSWFoIQZHl6pleBh6suxKMIhlvzbAwkBWfFWrBQTxNLq2RG2yhSUkDs2b63AYDAoJXAcFRwADeAkJ" +
            "DX0AQCsEfAQMDAIPBz0rCgcxky0JRWE1AmwpKyEAIfkECQoAAAAsAAAAABAAEAAABXkgIAICKZzkqJ4nQZx" +
            "LqZKv4NqNLKK2/Q4Ek4lFXChsg5ypJjs1II3gEDUSRInEGYAw6B6zM4JhrDAtEosVkLUtHA7RHaHAGJQEjs" +
            "ODcEg0FBAFVgkQJQ1pAwcDDw8KcFtSInwJAowCCA6RIwqZAgkPNgVpWndjdyohACH5BAkKAAAALAAAAAAQA" +
            "BAAAAV5ICACAimc5KieLEuUKvm2xAKLqDCfC2GaO9eL0LABWTiBYmA06W6kHgvCqEJiAIJiu3gcvgUsscHU" +
            "ERm+kaCxyxa+zRPk0SgJEgfIvbAdIAQLCAYlCj4DBw0IBQsMCjIqBAcPAooCBg9pKgsJLwUFOhCZKyQDA3Y" +
            "qIQAh+QQJCgAAACwAAAAAEAAQAAAFdSAgAgIpnOSonmxbqiThCrJKEHFbo8JxDDOZYFFb+A41E4H4OhkOip" +
            "XwBElYITDAckFEOBgMQ3arkMkUBdxIUGZpEb7kaQBRlASPg0FQQHAbEEMGDSVEAA1QBhAED1E0NgwFAooCD" +
            "WljaQIQCE5qMHcNhCkjIQAh+QQJCgAAACwAAAAAEAAQAAAFeSAgAgIpnOSoLgxxvqgKLEcCC65KEAByKK8c" +
            "SpA4DAiHQ/DkKhGKh4ZCtCyZGo6F6iYYPAqFgYy02xkSaLEMV34tELyRYNEsCQyHlvWkGCzsPgMCEAY7Cg0" +
            "4Uk48LAsDhRA8MVQPEF0GAgqYYwSRlycNcWskCkApIyEAOwAAAAAAAAAAAA==";
    }
}

ImageAnimator.StopAnimate() 似乎并没有真正做任何事情

我反编译了该类,发现 ImageAnimator 启动了线程工作器:

 /// <devdoc> 
    ///     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.
    /// </devdoc> 
    [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals")] 
    static void AnimateImages50ms() {
        Debug.Assert(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.
            rwImgListLock.AcquireReaderLock(Timeout.Infinite); 
            try { 
                for (int i=0;i < imageInfoList.Count; i++) {
                    ImageInfo imageInfo = 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 ){ 
                            anyFrameDirty = true;
                        }
                    }
                } 
            }
            finally { 
                rwImgListLock.ReleaseReaderLock(); 
            }
            Thread.Sleep(50);
        }
    }

一旦它开始,它看起来没有任何办法让它停止,实际上很糟糕。

我遇到了一个问题,即带有动画 gif 图像的 PictureBox 控件会导致应用程序退出时挂起。

罪魁祸首是静态 ImageAnimator 类创建了一个处于 while(true) 循环中的线程。

幸运的是,ImageAnimator 将用于私有字段的线程设置为私有字段。它可以通过反射和中止来访问,也可以停止动画。

这是我的代码,我已经确认中止线程,动画停止。


    try
    {
       var type = typeof(ImageAnimator);
       var info = type.GetField("animationThread", BindingFlags.NonPublic | BindingFlags.Static);
       if (info != null)
       {
           var value = info.GetValue(null);
           var thread = value as Thread;
           if (thread != null && thread.IsAlive)
           {
                thread.Abort();
           }
        }
    }
    catch{}

您的代理人未被取消订阅的原因是ImageAnimator将委托人地址与您最初订阅的代理人的地址进行比较。 您传递给 StopAnimate 的委托与您传递给 Animate 的委托不同。

这是StopAnimate中的相关代码块:

try {
     // Find the corresponding reference and remove it
     for(int i = 0; i < imageInfoList.Count; i++) {
         ImageInfo imageInfo = imageInfoList[i];
         if(image == imageInfo.Image) {
             if((onFrameChangedHandler == imageInfo.FrameChangedHandler) || (onFrameChangedHandler != null && onFrameChangedHandler.Equals(imageInfo.FrameChangedHandler))) {
                            imageInfoList.Remove(imageInfo);
              }
                  break;
              }
          }
}

请注意onFrameChangedHandler的比较。

ImageAnimator使用单个线程,因此应用程序中的所有动画图像都可以由单个线程进行动画处理。 如果终止工作线程,应用中的所有动画图像都将停止动画处理。 如果您只有一个,但在大多数情况下可能不是您想要的,这可能很好。

AnimateStopAnimate工作正常;你只需要正确调用它们。