I read about delegates and event driven programming but could'nt understand it completely.Can somebody plz tell me taking some real example that what are delegates ,what are events and event driven programming and then finally how are these delegates and event driven programming connected to each other......I know the basics of delegates and event driven programming but could'nt figure out exactly how these things work ...Any help is much appreciated
static void Main(string args)
// Create instance of our EventExample class,
// giving it a 1 second interval for its internal timer.
EventExample eventExample = new EventExample(1000);
// Register for
eventExample.LoadFinished += new EventHandler(eventExample_LoadFinished);
// Handler for our LoadFinished event.
static void eventExample_LoadFinished(object sender, EventArgs e)
public class EventExample
public event EventHandler LoadFinished;
protected virtual void OnLoadFinished(EventArgs e)
// Thread safe pattern.
EventHandler handler = LoadFinished;
// If we have any listeners registered to this event...
if (handler != null)
// Notify them!
public EventExample(double timerInterval)
mTimer = new Timer(timerInterval);
// Method to start a timer. Timer will send out event to any listener when finished.
public void StartLoad()
// Setup Timer and start it.
mTimer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
mStartDateTime = DateTime.Now;
// Handler for our Timer.
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
// If 5 seconds have elapsed...
if ((e.SignalTime - mStartDateTime).Seconds > 5)
// Stop the timer.
// Notify listeners!
private Timer mTimer;
private DateTime mStartDateTime;
// delegate to a method that returns a void and takes a string.
public delegate void PrintString(string s);
static void Main(string args)
// Assign our delegate to the static method of our DelegateExample class.
// This basically assigns our delegate instance to point to the method we want.
// When our delegate is invoked, it will invoke the method is is pointing to.
PrintString func = new PrintString(DelegateExample.SomeFucntion);
// Invoke our delegate.
public class DelegateExample
public static void SomeFucntion(string s)
I know the basics of delegates and event driven programming but could'nt figure out exactly how these things work ...Any help is much appreciated
So if you know the basics whats the problem? What exactly you dont understand in the words event, delegate and driven ?
In C++ we had messages, and that messages controls the program. each Mouse move creates a message and the message was used to influence what the program shuld do when the mouse moves over a specific control and so on. Now this is the same with events and delegates. each mouse move is an event and the delegate which receives that event will react on that event and that way influence what should happen, when the mouse moves. So simple clear up, what the words mean and you will get the whole idea. Understanding design concepts are normally not depending on examples, especially as you said, you basically know about what it is. Understanding in the first place depends on exactly understanding the language terms. What is an event in real live. So what is the term meaning in programming. What is a delegate meaning in real life? OK. Then what does it mean in programming language. So an event should be fired, when something happens, which should create a reaction elsewhere in the program. There you have a delegate waiting if the event is fired. If it is fired, the delegate reacts. Thats all about.
One simple examople:
You have two forms and want to change the text on form 2 depending on changes of a textbox on form1. Ok. You cannot couple them permanent because maybe sometimes you only open Form 1 and form 2 is still closed. No problem, using an event and a delegate. If form2 is not created, form 1 still sends the changes of its textbox, form 2 simple is not opened so the information is not received, no problem. As soon you open form 2 the delegate will be informed about all changes of the text in form1.
Hopefully that cleared up your problem.
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ? My latest articles : Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
In C++ we had messages, and that messages controls the program. each Mouse move creates a message and the message was used to influence what the program shuld do when the mouse moves over a specific control and so on.
I cannot accept this. messages do not control the program. they only notify me the something has happed so that I can "properly" react to this "event". I control the program (or the system) I'd also prefer that when the mouse moves the system sends a proper notification
@sidhu688 - you should exacly tell us what you have a problem with.
Last edited by memeloo; February 7th, 2010 at 12:45 AM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
The following simple example shows a class, ListWithChangedEvent, which is similar to the standard ArrayList class, but also invokes a Changed event whenever the contents of the list change. Such a general-purpose class could be used in numerous ways in a large program.