delegates and event driven programming
Hello guys,
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
Thanks
Sid
Re: delegates and event driven programming
Event Example:
Code:
namespace EventsExample
{
using System;
using System.Timers;
class Program
{
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);
eventExample.StartLoad();
Console.ReadLine();
}
// Handler for our LoadFinished event.
static void eventExample_LoadFinished(object sender, EventArgs e)
{
Console.WriteLine("Load Finished");
}
}
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!
handler(this, e);
}
}
// Constructor.
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()
{
Console.WriteLine("Load started");
// Setup Timer and start it.
mTimer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
mTimer.Start();
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.
mTimer.Stop();
// Notify listeners!
OnLoadFinished(EventArgs.Empty);
}
}
private Timer mTimer;
private DateTime mStartDateTime;
}
}
Delegate Example:
Code:
namespace DelegatesExample
{
using System;
// delegate to a method that returns a void and takes a string.
public delegate void PrintString(string s);
class Program
{
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.
func("Hello World");
Console.ReadLine();
}
}
public class DelegateExample
{
public static void SomeFucntion(string s)
{
Console.WriteLine(s);
}
}
}
Re: delegates and event driven programming
Quote:
Originally Posted by
sidhu688
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.
Re: delegates and event driven programming
Quote:
Originally Posted by
JonnyPoet
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 :wave:
@sidhu688 - you should exacly tell us what you have a problem with.
Re: delegates and event driven programming
This is a great example of events:
Quote:
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.
http://msdn.microsoft.com/en-us/libr...39(VS.71).aspx