Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Remove Event Handler after is called or call it just once

Tags:

c#

events

_cefGlueBrowser.LoadEnd += (s, e) =>
{
    BeginInvoke(new Action(() =>
    {
         MyCefStringVisitor visitor = new MyCefStringVisitor(this, m_url);
         e.Browser.GetMainFrame().GetSource(visitor);
         loaded = true;
    }));
};

But problem is that Event Handler is called many times. After each JS reload for example. How to remove multiple calls. How to call LoadEnd event just once.

I try with

_cefGlueBrowser.LoadEnd -= delegate { };

but not working.

What can i do? I want to call it just once!

like image 380
mbrc Avatar asked Dec 21 '13 09:12

mbrc


1 Answers

EventHandler handler = null;
obj.SomeEvent += handler = (s, e) => {
    obj.SomeEvent -= handler;
    // more stuff
};

This works because it is the variable that us captured (lexical closure), not the value of the variable at any particular time.

like image 196
Marc Gravell Avatar answered Sep 18 '22 18:09

Marc Gravell