
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to subscribe to an Event in C# and can we have multiple subscribers to one Event in a C#?
Events enable a class or object to notify other classes or objects when something of interest occurs.
The class that raises the event is called the publisher and the classes that handle the event are called subscribers.
In the Event
An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers.
Events that have no subscribers are never raised.
The publisher determines when an event is raised; the subscribers determine what action is taken in response to the event.
Example
class Program { static void Main() { var video = new MP4() { Title = "Eminem" }; var videoEncoder = new MP4EncoderNew(); var mailService = new MailService(); var messageService = new MessageService(); videoEncoder.mp4Encoded += mailService.onVideoEncoded; videoEncoder.mp4Encoded += messageService.onVideoEncoded; videoEncoder.Encode(video); Console.ReadKey(); } } public class MP4 { public string Title { get; set; } } public class MP4Args : EventArgs { public MP4 mp4 { get; set; } } public class MP4EncoderNew { public EventHandler mp4Encoded; public void Encode(MP4 video) { Console.WriteLine("Encoding MP4"); Thread.Sleep(3000); OnVideoEncoded(video); } protected void OnVideoEncoded(MP4 video) { if (mp4Encoded != null) { mp4Encoded(this, new MP4Args() { mp4 = video }); } } } //Subscriber 1 public class MailService { public void onVideoEncoded(object source, MP4Args e) { Console.WriteLine("Mail Service:,Sending an email {0}", e.mp4.Title); } } //Subscriber 2 public class MessageService { public void onVideoEncoded(object source, MP4Args e) { Console.WriteLine("Message Service:,Sending an Message {0}", e.mp4.Title); } }
Output
Encoding MP4 Mail Service:,Sending an email Eminem Message Service:,Sending an Message Eminem
- Related Articles
- How can we modify an existing MySQL event?
- How can we RENAME an existing MySQL event?
- How to call multiple JavaScript functions in onclick event?
- How can we delete an existing MySQL event permanently?
- How can we create a MySQL one-time event that executes immediately?
- How to declare an event in C#?
- How can we ENABLE AND DISABLE a particular MySQL event?
- How can we create multiple MySQL triggers for the same trigger event and action time?
- How can we start MySQL event scheduler?
- How to use multiple click event on HTML5 canvas?
- Can I wrap a JavaScript event in a jQuery event?
- How can I move an existing MySQL event to another database?
- How to pass event objects from one function to another in JavaScript?
- How to pass an argument to the event handler in Tkinter?
- How to trigger event in JavaScript?

Advertisements