惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
D
DataBreaches.Net
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
F
Full Disclosure
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Help Net Security
Help Net Security
L
LangChain Blog
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
Google Online Security Blog
Google Online Security Blog
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
B
Blog RSS Feed
N
Netflix TechBlog - Medium
N
News | PayPal Newsroom
TaoSecurity Blog
TaoSecurity Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
B
Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AI
AI
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
G
GRAHAM CLULEY
Vercel News
Vercel News
罗磊的独立博客
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
博客园 - 司徒正美
C
CERT Recently Published Vulnerability Notes
GbyAI
GbyAI
Scott Helme
Scott Helme
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
A
About on SuperTechFans
P
Privacy International News Feed

博客园 - 恭喜发财

android 电话状态的监听(来电和去电) (转)Xml DML - 恭喜发财 - 博客园 (转)sql:variable() binding and modify method of xquery: insert XML DML SQL Server Backup file in standard Zip format Working with binary large objects (BLOBs) c# 得到局域网中可用SqlServer服务器列表(转) C#实现UDP协议(转) 健康大讲堂—凡膳皆药 寓医于食 ReportView(RDSL)参考资料 How To Print Using Custom Page Sizes on Windows NT and Windows 2000(VB6) 汇总c#.net常用函数和方法集 在多线程中如何调用Winform 使用C#在进度条中显示复制文件的进度(转) c#将大文件读取或写入到数据库(带进度条的源码)(转) 写C#自定义控件的心得 Finalize(析构函数)、Dispose、Close 的区别与使用 如何在WinForm中对DataGrid进行分页显示(转) 如何用C#做一个类似于桌面插件的程序(转) c#连接各类数据库大全
在WinForm中控制GIF动画的启停的一种方法(转)
恭喜发财 · 2009-05-04 · via 博客园 - 恭喜发财

在WinForm中许多控件(本文以最常用的PictureBox为例)都有Image属性,如果该Image是一个动画Gif的话,它会自动运行动画。在某些场合,我们希望控制该动画的启停,此时我们首先想到了ImageAnimator,但实际使用时,我们发现以下指令根本不起作用,Gif停不下来,具体原因不明:

        ImageAnimator.StopAnimate(box.Image, new EventHandler(OnImageAnimate));
        private void OnImageAnimate(Object sender, EventArgs e)
        {
            this.Invalidate();
        }
    此时,我想到控件的Enable属性,在其为false时,Gif是不会启动的,一旦设其为true,则动画立刻自动运行,因此只要该属性不作它用,则完全可以用来控制动画的启停。
    需要注意的是线程的安全性,以下代码示例实现了某一控件点击后启动Gif运行5秒钟,然后停下来:

        delegate void SetGifEnableCallback(Control control, bool enable);

        void picboxSearching_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(Animate));
            t.Start();
        }

        private void SetGifEnable(Control control, bool enable)
        {
            if (control.InvokeRequired)
            {
                SetGifEnableCallback callback = new SetGifEnableCallback(SetGifEnable);
                this.Invoke(callback, new object[] { control, enable });
            }
            else control.Enabled = enable;
        }

        private void Animate()
        {
            PictureBox box = this.pictureBox1;
            AutoResetEvent waitHandler = new AutoResetEvent(false);
            SetGifEnable(box, true);
            waitHandler.WaitOne(5000, true);
            SetGifEnable(box, false);
        }