CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2006
    Posts
    146

    I need quick, simple explanation on this Delegate/Event code..

    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

    Code:
    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.

    Code:
        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
    Code:
            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
    Code:
    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 });
            }
    Code:
     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
    Last edited by messycan; December 27th, 2007 at 11:19 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: I need quick, simple explanation on this Delegate/Event code..

    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
    Code:
    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
    Code:
     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 )
    Jonny Poet

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured