Async Events vs Async Delegates vs Async Mixed

Arsium ***** 331 Reputation points
2022-11-24T16:49:15.523+00:00

Just curious about difference(s) between those 3 different codes (and why choosing one /vs others) (only the 'CallThem' method is used when instancied) :

        public class Test  
        {  
            public event EventHandler<TestEventArgs> Event1;  
            public event EventHandler<TestEventArgs> Event2;  
  
            public Test() : base()  
            {  
                Event1 += M;  
                Event2 += M2;  
            }  
  
            public void CallThem()   
            {  
                Event1.BeginInvoke(this, new TestEventArgs("Test 1"), new AsyncCallback(AsyncRes1), "Test 1 End");  
                Event2.BeginInvoke(this, new TestEventArgs("Test 2"), new AsyncCallback(AsyncRes2), "Test 2 End");  
            }  
  
            private void M(object sender, TestEventArgs e)   
            {  
                MessageBox.Show(e.msg);  
            }  
  
            private void M2(object sender, TestEventArgs e)  
            {  
                MessageBox.Show("Second call : " + e.msg);  
            }  
  
            private void AsyncRes1(IAsyncResult ar)   
            {  
                string r = (string)ar.AsyncState;  
                MessageBox.Show(r);  
            }  
  
            private void AsyncRes2(IAsyncResult ar)  
            {  
                string r = (string)ar.AsyncState;  
                MessageBox.Show("AsyncRes2 : " + r);  
            }  
        }  
  
        public class Test2  
        {  
            public delegate void TestEventHandler(object sender, TestEventArgs e);  
            private TestEventHandler Del1;  
            private TestEventHandler Del2;  
  
            public Test2() : base()  
            {  
                Del1 += M;  
                Del2 += M2;  
            }  
  
            public void CallThem()  
            {  
                Del1.BeginInvoke(this, new TestEventArgs("Test 1"), new AsyncCallback(AsyncRes1), "Test 1 End");  
                Del2.BeginInvoke(this, new TestEventArgs("Test 2"), new AsyncCallback(AsyncRes2), "Test 2 End");  
            }  
  
            private void M(object sender, TestEventArgs e)  
            {  
                MessageBox.Show(e.msg);  
            }  
  
            private void M2(object sender, TestEventArgs e)  
            {  
                MessageBox.Show("Second call : " + e.msg);  
            }  
  
            private void AsyncRes1(IAsyncResult ar)  
            {  
                string r = (string)ar.AsyncState;  
                MessageBox.Show(r);  
            }  
  
            private void AsyncRes2(IAsyncResult ar)  
            {  
                string r = (string)ar.AsyncState;  
                MessageBox.Show("AsyncRes2 : " + r);  
            }  
        }  
  
        public class Test3  
        {  
            public delegate void TestEventHandler(object sender, TestEventArgs e);  
            private event TestEventHandler DelEvent1;  
            private event TestEventHandler DelEvent2;  
  
            public Test3() : base()  
            {  
                DelEvent1 += (sender, e ) => { MessageBox.Show(e.msg); };  
                DelEvent2 += M2;  
            }  
  
            public void CallThem()  
            {  
                DelEvent1.BeginInvoke(this, new TestEventArgs("Test 1"), new AsyncCallback(AsyncRes1), "Test 1 End");  
                DelEvent2.BeginInvoke(this, new TestEventArgs("Test 2"), new AsyncCallback(AsyncRes2), "Test 2 End");  
            }  
  
            private void M2(object sender, TestEventArgs e)  
            {  
                MessageBox.Show("Second call : " + e.msg);  
            }  
  
            private void AsyncRes1(IAsyncResult ar)  
            {  
                string r = (string)ar.AsyncState;  
                MessageBox.Show(r);  
            }  
  
            private void AsyncRes2(IAsyncResult ar)  
            {  
                string r = (string)ar.AsyncState;  
                MessageBox.Show("AsyncRes2 : " + r);  
            }  
        }  
Developer technologies Windows Forms
Developer technologies C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-11-25T08:45:31.38+00:00

    @Arsium ***** , Welcome to Microsoft Q&A,

    Just curious about difference(s) between those 3 different codes (and why choosing one /vs others)

    The three different codes are all used to define two events and add some methods to trigger the events.

    Test class only defined event.

    Test2 class only defined delegate.

    Test3 class converted delegate to event.

    Hope my explanation could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.