Synchronous and Asynchronous use of Delegate with C#

By | January 6, 2018

For developers the quality of a program can be often express in the time used to finish the computation. With current multicore processor we have to move our thinking from a serial execution to a concurrent execution. Using delegates in an asynchronous way can force the CLR to allocate multiple threads to your computation.

In below code sample, you will see that I call one method multiple times. This method simply waits 5 seconds. Calling 6 times this method in a synchronous way makes the time to finish obvious: at least 30 seconds! Now using the BeginInvoke method from my delegate I can add multiple threads to these computations. On my machine the time to achieve this is now 9 seconds only!

Delegate can also be used to call method from different classes. I let you check at the documentation on Msdn.

 
2 Kudos
Don't
move!

11 thoughts on “Synchronous and Asynchronous use of Delegate with C#

  1. Naseet

    A very good article to clear my doubts on sync and async method calling from delegate

    Reply
  2. prassana kumar

    through the main thread execution i created a delegate to invoke but at the time of closing i am getting exception objectdispose exception what should i do

    Reply

Thoughts?