Click to See Complete Forum and Search --> : I need quick, simple explanation on this Delegate/Event code..


messycan
December 27th, 2007, 10:14 PM
Ok, I downloaded a sample multithreaded CLient/Server TCP/IP chat project..I got hung up about delegates at first, but then I was soon to understand the basic concept.

Here is what I am wondering.

In the client class, I simply see


private delegate void CloseConnectionCallback(string strReason);

...

// Call this to prevent cross-thread calls
this.Invoke(new CloseConnectionCallback(this.CloseConnection), new object[] { Reason });


Simple. I understand this. All the code is in one class.

Now, for the part that I do not understand is in the Server project.

The server project has multiple classes, so i am assuming this is needed to fire an even across MULTIPLE classes.


public delegate void StatusChangedEventHandler(object sender, StatusChangedEventArgs e);

public class StatusChangedEventArgs : EventArgs
{
private string EventMsg;

public string EventMessage
{
get
{
return EventMsg;
}
set
{
EventMsg = value;
}
}

// Constructor
public StatusChangedEventArgs(string strEventArgs)
{
EventMsg = strEventArgs;
}
}


Now a different class

public static event StatusChangedEventHandler StatusChanged;
public static StatusChangedEventArgs e;

public static void OnStatusChanged(StatusChangedEventArgs e)
{
StatusChangedEventHandler statusHandler = StatusChanged;

if (statusHandler != null)
{
statusHandler(null, e);
}
}

// To fire the event

e = new StatusChangedEventArgs("User: " + strPacketData );
OnStatusChanged(e);


Yet a diferent class

private delegate void UpdateStatusCallback(string strMessage);

public ChatServer()
{
InitializeComponent();
}


public void server_StatusChanged(object sender, StatusChangedEventArgs e)
{
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
}




Server.StatusChanged += new StatusChangedEventHandler(server_StatusChanged);


Now, why not just use the simple method as in the first example? Am I right assuming that this is needed to fire events from multiple classes? Enlighten me :)

JonnyPoet
December 28th, 2007, 01:01 PM
Hi !

In the client Class you simple hve the delegate which is activated when CloseConnection is fired.

In the serverclass instead of this you have ti fire all the needed events obviously

So you need to have a StatusChangedEventArgument which is built by n EventArgsClass named StatusChangedEventArgs and a you see its inherited from EventArgs. In Žny case when you want to transport different messages around yu need different EventArgs classes, you all can derive them from EventArgs class and do in your needed classes, Fields, whateveer.
The OnStatusChanged method is the normal standard way to fire an event . I cannot see alll the different classes in your code asyou havn't mentions of which class they are part ( I'm talking about the last three code sections in your code example ) But the line
Server.StatusChanged += new StatusChangedEventHandler(server_StatusChanged); totally is used to add a delegate to a server class I think its exactly the ChatServer class. And this is then code in this Chat Server cass then to fire the event
public static event StatusChangedEventHandler StatusChanged;
public static StatusChangedEventArgs e;

public static void OnStatusChanged(StatusChangedEventArgs e)
{
StatusChangedEventHandler statusHandler = StatusChanged;

if (statusHandler != null)
{
statusHandler(null, e);
}
}
So this should fit together. For better understanding of delegates see y detailed and illustrated explanation about how classes are communicating to each other using delegaes. This is in part 3 of my series about how to create a dockingControler. See in the bottom of this post under m signture there you will finf the link. ( If you find it helpful you are invited to vote for it, as there is just a competition running about best of november articles. ( The yellow line in the forums header invites for votings )