none
threading vs asynchronous delegate RRS feed

  • Question

  • For doing parallel programming which is the most effective way... using Multi-threading or by using asynchronous delegates...?
    Saturday, October 20, 2012 8:09 AM

Answers

  • Hi Gopi,

    Generally, there is not much difference between calling function using async delegates and creating new thread for the function.

    If you use async delegate and you call the BeginInvoke method, then the request is queued by the CLR and BeginInvoke immediatelly returns. Then CLR will use system ThreadPool to get the free thread and runs your request on that thread. When you function ends, the CLR will call callback specified in the BeginInvoke method. There you can get info about the result of your function...

    So the only difference between using async callback and threads is that your own created thread is running outside of the system ThreadPool...

    The question when it is better to use async callback and when thread is not so clear. It is the same... The first one uses the second one. However If you have standalone function already written and all you need to do is to call it asynchronously, then use async delegates. However, if you need to get more control over the thread execution or the job you want to make asynchronous if more complex and it is built from more functions or you don't want to get thread for this task from system ThreadPool (the number of parallel threads is limited there), then use threads directly.


    Regards, http://www.shwetalodha.blogspot.in/

    • Proposed as answer by Shweta Jain (Lodha) Saturday, October 20, 2012 8:47 AM
    • Marked as answer by Gopi S V Sunday, October 21, 2012 7:26 AM
    Saturday, October 20, 2012 8:46 AM

All replies

  • I got a answer for this by searching more about the comparison. 

    But what i really needed is, which is best among asynchronous delegate operation and doing the same using threading.

    A response from IanG speaks about the number of thread created for asynchronous operation and normal threading is compared. But performance and in memory utilization perspective i need an advantage and disadvantage comparison.


    Saturday, October 20, 2012 8:25 AM
  • Hi Gopi,

    Generally, there is not much difference between calling function using async delegates and creating new thread for the function.

    If you use async delegate and you call the BeginInvoke method, then the request is queued by the CLR and BeginInvoke immediatelly returns. Then CLR will use system ThreadPool to get the free thread and runs your request on that thread. When you function ends, the CLR will call callback specified in the BeginInvoke method. There you can get info about the result of your function...

    So the only difference between using async callback and threads is that your own created thread is running outside of the system ThreadPool...

    The question when it is better to use async callback and when thread is not so clear. It is the same... The first one uses the second one. However If you have standalone function already written and all you need to do is to call it asynchronously, then use async delegates. However, if you need to get more control over the thread execution or the job you want to make asynchronous if more complex and it is built from more functions or you don't want to get thread for this task from system ThreadPool (the number of parallel threads is limited there), then use threads directly.


    Regards, http://www.shwetalodha.blogspot.in/

    • Proposed as answer by Shweta Jain (Lodha) Saturday, October 20, 2012 8:47 AM
    • Marked as answer by Gopi S V Sunday, October 21, 2012 7:26 AM
    Saturday, October 20, 2012 8:46 AM
  • Thanks a lot Shweta!

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/fa9e1830-ef06-4dd1-8ef7-59ebf04ab1e6

    The above given link is where i found that asynchronous delegate will use minimum number of threads, since it creates a new thread only when a call back is needed and executes with the existing thread when there is no need for a call back.

    But on the other hand, explicitly creating threads our self for a complex requirement, might result in overhead.

    Please correct me if am wrong.

    Thanks again for your valuable response.

    Also, this link gives further more input 

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a328d794-56cc-46c0-b039-c91c3cd5896b

    • Edited by Gopi S V Sunday, October 21, 2012 9:22 AM
    Sunday, October 21, 2012 7:30 AM