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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
爱范儿
爱范儿
GbyAI
GbyAI
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Secure Thoughts
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
罗磊的独立博客
Y
Y Combinator Blog
Know Your Adversary
Know Your Adversary
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recorded Future
Recorded Future
S
Securelist
T
Tor Project blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
P
Privacy & Cybersecurity Law Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Project Zero
Project Zero
T
Tailwind CSS Blog
腾讯CDC
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
F
Full Disclosure
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
美团技术团队
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
C
Cybersecurity and Infrastructure Security Agency CISA
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
O
OpenAI News
Cloudbric
Cloudbric

博客园 - 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);                       

                    });

        }