示例#1
0
        //Step 3:
        //Method Responsible for Raising Event (Notify registered objects that event occurred)
        //by convention protected virtual void (so inheritance is possible, if class is sealed - private, nonvirtual)
        protected virtual void OnNewMail(MailEventArgs e)
        {
            //variant 1
            //not thread safe
            //if (NewMail != null) NewMail(this, e);

            //variant 2
            //Thread safe in theory but compiler could optimize it
            //EventHandler<MailEventArgs> TempNewMail = NewMail;
            //if (TempNewMail != null) TempNewMail(this, e);

            //variant 3
            //according to Rihter
            //EventHandler<MailEventArgs> TempNewMail = Volatile.Read(ref NewMail);
            //if (TempNewMail != null) TempNewMail(this, e);

            //variant 4
            //according to C#6.0 - it is thread safe
            NewMail?.Invoke(this, e);
        }
示例#2
0
        private void FaxMsg(Object sender, MailEventArgs e)
        {
            //'sender' identifies the MailManager object in case we want communicate back to it

            Console.WriteLine("FAX received message from {0}, to {1}", e.From, e.To);
        }
示例#3
0
        //Step 4:
        //Method that translates the input into the desired event
        public void ReceiveEmail(String From, String To)
        {
            MailEventArgs e = new MailEventArgs(From, To);

            OnNewMail(e);
        }
示例#4
0
 private void PcMsg(object sender, MailEventArgs e)
 {
     Console.WriteLine("PC received message from {0}, to {1}", e.From, e.To);
 }