CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  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.

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