A small demo from the evolution of the C # Via CLR explores the principle of adding and removing events that is straightforward and straightforward:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading;6 usingSystem.Threading.Tasks;7 8 namespaceExpForEvent0019 {Ten Public Sealed classEventkey:object {} One A Public Sealed classEventset - { - Private ReadOnlyDictionary<eventkey, delegate> m_events =NewDictionary<eventkey, delegate>(); the - Public voidAdd (Eventkey eventkey, Delegate handler) - { - Monitor.Enter (m_events); + Delegate D; -M_events. TryGetValue (Eventkey, outd); +M_events[eventkey] = Delegate.combine (d, Handler);//Here we used Combine A Monitor.Exit (m_events); at } - - Public voidRemove (Eventkey eventkey, Delegate handler) - { - Monitor.Enter (m_events); - Delegate D; in if(M_events. TryGetValue (Eventkey, outd)) - { toD =Delegate.remove (d, handler); + - if(d! =NULL) M_events[eventkey] =D; the Elsem_events. Remove (Eventkey); * } $ Monitor.Exit (m_events);Panax Notoginseng } - the Public voidRaise (eventkey eventkey, Object sender, EventArgs e) + { A Delegate D; the Monitor.Enter (m_events); +M_events. TryGetValue (Eventkey, outd); - Monitor.Exit (m_events); $ $ if(d! =NULL) - { -D.dynamicinvoke (Newobject[]{sender, E}); the } - }Wuyi } the - Public classFooeventargs:eventargs {} Wu - Public classtypewithlotsofevents About { $ Public ReadOnlyEventset M_eventset =NewEventset (); - - protectedEventset Eventset {Get{returnM_eventset;} } - A #regionSupport Foo Event + the protected Static ReadOnlyEventkey S_fooeventkey =NewEventkey (); - $ Public EventEventhandler<fooeventargs>Foo the { the Add {m_eventset.add (S_fooeventkey, value);} the Remove {m_eventset.remove (S_fooeventkey, value);} the } - in protected Virtual voidOnFoo (Fooeventargs e) the { theM_eventset.raise (S_fooeventkey, This, e); About } the the Public voidSimulatefoo () {OnFoo (NewFooeventargs ()); } the + #endregion - } the Bayi class Program the { the Static voidMain (string[] args) - { -Typewithlotsofevents Twle =Newtypewithlotsofevents (); the theTwle. Foo + =HandleFooEvent1; theTwle. Foo + =HandleFooEvent2; the - Twle. Simulatefoo (); the } the the Private Static voidHandleFooEvent1 (Objectsender, Fooeventargs e)94 { theConsole.WriteLine ("handling Foo Event here ..."); the } the 98 Private Static voidHandleFooEvent2 (Objectsender, Fooeventargs e) About { -Console.WriteLine ("handling Foo Event here2 ...");101 }102 }103}
View Code
Operation Result:
A passage with Richter the Great God:
The JIT compiler can tell if the code is running on an Intel Pentium 4 CPU, and generates the appropriate local code to take advantage of any "special" instructions that Pentium 4 supports, whereas unmanaged code is typically compiled for a generic, minimal-function set of CPUs. Special instructions for boosting application performance are not used. ”
My words:
There are differences in the instruction set of computers with different architectures. The more abstract the software is, the farther away it is from the hardware, which blocks the view that we see the real problem.
Simulating event additions and deletions in C #