

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How can we modify an existing MySQL event?
- How can we RENAME an existing MySQL event?
- How can we create a MySQL one-time event that executes immediately?
- How can we delete an existing MySQL event permanently?
- How can we ENABLE AND DISABLE a particular MySQL event?
- How can we start MySQL event scheduler?
- How to declare an event in C#?
- How to call multiple JavaScript functions in onclick event?
- Can I wrap a JavaScript event in a jQuery event?
- How can we create multiple MySQL triggers for the same trigger event and action time?
- How to use multiple click event on HTML5 canvas?
- How can I move an existing MySQL event to another database?
- How to pass event objects from one function to another in JavaScript?
- How can we create a MySQL one-time event that executes after some specified time interval?
- How to trigger event in JavaScript?
Advertisements