C # multiple event types

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. componentmodel;
Using system. runtime. compilerservices;

Namespace net. mst. Sixth. multieventsclass
{
Class mainclass
{
Static void main (string [] ARGs)
{
// Trigger a Test Event
Using (multieventsclass c = new multieventsclass ())
{
Customer customer = new customer (C );
C. riseevent1 ();
C. riseevent2 ();
}
Console. Read ();
}
}

/// <Summary>
/// Multiple event types
/// </Summary>
Public partial class multieventsclass: idisposable
{
/// <Summary>
/// System. componentmodel. eventhandlerlist contains a delegate linked list container
/// Implements the packaging of multiple events stored in a container
/// Eventhandlerlist uses the Linked List Data Structure
/// </Summary>
Private eventhandlerlist _ events;

// Public Constructor
Public multieventsclass ()
{
_ Events = new eventhandlerlist ();
}
/// <Summary>
/// Release eventhanlderlist
/// </Summary>
Public void dispose ()
{
_ Events. Dispose ();
}
}
/// <Summary>
/// Multiple event types
/// </Summary>
Public partial class multieventsclass: idisposable
{
// For each event to be implemented, declare the delegate type, subscribe and cancel the attribute method, the key of the event in the collection, and the trigger event method.
// This definition is different from the actual declaration of an event member. In this way, the memory space of all event Delegate linked lists will not be allocated in a new multieventsclass.
// This is the key to improving performance
// Declare event 1
# Region event1
// Delegate prototype of Event 1
Public Delegate void event1handler (Object sender, eventargs E );
// Here is a static field, effectively improving performance
Protected static readonly object event1key = new object ();
/// <Summary>
/// A group of subscription and cancellation Methods
/// Note that eventhandlerlist does not provide thread synchronization, so the thread synchronization attribute is added before the add and remove methods.
/// The reader can use the lock mechanism instead.
/// </Summary>
Public event event1handler event1
{
[Methodimpl (methodimploptions. Synchronized)]
Add
{
_ Events. addhandler (event1key, value );
}
[Methodimpl (methodimploptions. Synchronized)]
Remove
{
_ Events. removehandler (event1key, value );
}
}
/// <Summary>
/// Trigger event 1
/// </Summary>
/// <Param name = "E"> </param>
Protected virtual void onevent1 (eventargs E)
{
_ Events [event1key]. dynamicinvoke (this, e );
}
/// <Summary>
/// This method simply triggers Event 1 for testing
/// </Summary>
Public void riseevent1 ()
{
Onevent1 (eventargs. Empty );
}
# Endregion
// Declare event 2
# Region event2
// Delegate prototype of event 2
Public Delegate void event2handler (Object sender, eventargs E );
// Here is a static field, effectively improving performance
Protected static readonly object event2key = new object ();
/// <Summary>
/// A group of subscription and cancellation Methods
/// Note that eventhandlerlist does not provide thread synchronization, so the thread synchronization attribute is added before the add and remove methods.
/// The reader can use the lock mechanism instead.
/// </Summary>
Public event event2handler event2
{
[Methodimpl (methodimploptions. Synchronized)]
Add
{
_ Events. addhandler (event2key, value );
}
[Methodimpl (methodimploptions. Synchronized)]
Remove
{
_ Events. removehandler (event2key, value );
}
}
/// <Summary>
/// Trigger event 2
/// </Summary>
/// <Param name = "E"> </param>
Protected virtual void onevent2 (eventargs E)
{
_ Events [event2key]. dynamicinvoke (this, e );
}
/// <Summary>
/// This method simply triggers event 2 for testing
/// </Summary>
Public void riseevent2 ()
{
Onevent2 (eventargs. Empty );
}
# Endregion
}

/// <Summary>
/// Construct a subscription Event Type
/// </Summary>
Public Class Customer
{
Public customer (multieventsclass events)
{
// Subscribe to Event 1
Events. event1 + = event1handler;
// Subscribe to event 2
Events. event2 + = event2handler;
}
/// <Summary>
/// Event 1 callback Method
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "ARGs"> </param>
Private void event1handler (Object sender, eventargs ARGs)
{
Console. writeline ("Event 1 triggered ");
}
/// <Summary>
/// Event 2 callback Method
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "ARGs"> </param>
Private void event2handler (Object sender, eventargs ARGs)
{
Console. writeline ("Event 2 triggered ");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.