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

推荐订阅源

Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
J
Java Code Geeks
V
Visual Studio Blog
腾讯CDC
博客园 - 【当耐特】
博客园 - 司徒正美
小众软件
小众软件
宝玉的分享
宝玉的分享
博客园 - Franky
量子位
有赞技术团队
有赞技术团队
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
N
News | PayPal Newsroom
D
Docker
Google Online Security Blog
Google Online Security Blog
博客园 - 聂微东
A
About on SuperTechFans
S
Security Affairs
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Securelist
T
The Exploit Database - CXSecurity.com
爱范儿
爱范儿
MyScale Blog
MyScale Blog
V
Vulnerabilities – Threatpost
S
Security @ Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Palo Alto Networks Blog

博客园 - Ellipse

Design by Contract what's interface Provider, Manager, Helper - Ellipse Build your own CAB #13 - Embedded Controllers with a Dash of DSL Layer supertype for MVP - Ellipse Build your own CAB#11 - Event Aggregator Build your own CAB Part #9 - Domain Centric Validation with the Notification Pattern Build your own CAB Part#8 - Assigning Responsibilities in a MVP pattern Build your own CAB Par#7 - what's the Model? Having a break Build your own CAB Part #6 - View to Presenter Communication Build your own CAB Part #4 - The Passive View Build your own CAB Part #3 - The Supervising Controller Pattern Build your own CAB Part #1 - The Preamble(Jeremy D. Miller) Build your own CAB Part #2 - The Humble Dialog Box -- Jeremy D. Miller Gridview 的RowCommand Reading Build your own CAB (1) -- Jeremy D. Miller ActionList Webpage redirection
Build your own CAB#12 - Rein in runaway events with the "Latch "
Ellipse · 2007-10-09 · via 博客园 - Ellipse

temporarily detach the event handler while you are performing some special actions

for example. When  you select a tabpage in a tabcontrol,  the TableSelected event will be rosed. But when you are creating a brand new table, you don't want the TableSelected event raised.

without the "latch", if you add a new item and do some initialization:
add a new item -> fire SelectedItemChanged -> event handler -> do initialization to the item

with the "latch"
add a new item (set the flag false) -> fire SelectedItemChanged -> (check the flag and not execute the event handler) event handler -> do initialization to the item

:
public
delegate void VoidHandler();

    public class Latch

    {

        private int _count = 0;

        public void Increment()

        {

            _count++;

        }

        public void Decrement()

        {

            _count--;

        }

        public bool IsLatched

        {

            get { return _count > 0; }

        }

        public void RunInsideLatch(VoidHandler handler)

        {

            Increment();

            handler();

            Decrement();

        }

        public void RunLatchedOperation(VoidHandler handler)

        {

            if (IsLatched)

            {

                return;

            }

            handler();

        }

There isn't too much to the usage.  In the method that performs work you might do this:

            _latch.RunInsideLatch(delegate

                                      {

                                          // The actions that spawn cascading events

                                          activatePresenter(presenter, page);

                                          _tabControl.Items.Add(page);

                                          _tabControl.SelectedTab = page;

                                      }

                );

In an event handler effected by this work you could guard the event propagation by doing this

        void TabControl_TabSelected(object sender, TabEventArgs args)

        {

            if (_latch.IsLatched)

            {

                return;

            }

            ContentTab tab = (ContentTab) TabControl.SelectedTab;

            activatePresenter(tab.Presenter, tab);

        }

or this version (fun with anonymous delegates):

        void TabControl_TabSelected(object sender, TabEventArgs args)

        {

            _latch.RunLatchedOperation(

                delegate

                    {

                        ContentTab tab = (ContentTab)TabControl.SelectedTab;

                        activatePresenter(tab.Presenter, tab);                       

                    });

        }