A Developer Journey who codes for fun

Daily Dose Of Code

  • Home
  • Dot.Net Basics
    • .Net Basics
      • CTS
      • CLS
      • CLR
      • Strong Vs Weak Ref
      • .Net Framework
      • What is Manifest
    • Memory Management
      • Garbage Collection 1
      • Garbage Collection 2
      • Circular Reference
  • C Sharp
    • Abstract Class in C#
    • Interfaces in C#
    • Value type by Val and By Ref
    • Var keyword
    • Null Coalescing Operator
    • Buit-in code snippets
  • OOPS
    • Abstraction and Encapsulation
    • Polymorphism
    • Inheritence
    • Aggregation
  • Threading
    • Delegates
      • Calling Delegate using Invoke, BeginInvoke
      • Multicast Delegate
      • Exception Handling in Multicast Delegate
      • Action
      • Predicate
      • Func
    • Synchronization
    • Thread Pool
    • Exception Handling
    • TPL
  • Design Pattern
    • Creational Patterns
      • Singleton Pattern
      • Factory Pattern
      • Abstract Factory Pattern
      • Prototype Pattern
      • Builder Pattern
    • Structural Patterns
      • Adapter Pattern
      • Bridge Pattern
      • Composite Pattern
      • Proxy Pattern
      • Facade Pattern
      • Decorator Pattern
      • Flyweight Pattern
    • Behavioral Patterns
      • Command Pattern
      • Interpreter Pattern
      • Iterator Pattern
      • Mediator Pattern
      • Memento Pattern
      • Observer Pattern
      • State Pattern
      • Strategy Pattern
      • Visitor Pattern
      • Chain Of Responsibility Pattern
      • Template Pattern
  • Data Structures
    • Generic List in C#
    • 2d array to 1d array
    • 3d arrayto 1d array
    • Linked List
      • Singly Linked List in C#
    • Queue
      • Dummy Data 1
    • Stack
      • Dummy Data 2
    • Tree
      • Dummy Data 3
    • Graph
      • Dummy Data 4
  • WCF
    • WCF Service using VS 2015
  • Scripts
    • Chrome Extensions
      • Create a Chrome Extension
      • Facebook autologout script
      • Gmail autologout script

Calling the Delegates using Invoke(), BeginInvoke() and DynamicInvoke() ?

 Unknown     11:59 PM     C#, Threading     15 comments   


Hello Guys, So in the last article we talked about What is delegate and how can we create a delegate.
In this article we will discuss what are the ways available to call a delegate and what are the differences in between ?
There are different ways in which you can invoke delegates to get a synchronous or an asynchronous behavior.










synchronous -  Using Invoke we can achieve this and in this delegate execute synchronously on the same thread. So in general when you want to invoke a delegate and want to wait for its completion before current thread continues then Invoke is the right choice. Details you can see in this article -


Asynchronous Way -  We can achieve this using BeginInvoke, It means you do not want that main thread  wait for the completion of the delegate. In this scenario the delegate and current thread can work in parallel.

Point to notice here is You can achieve Asynchronous  way using Delegate.BeginInvoke or creating a new thread then Which one to prefer and Why ?

Delegate provides a very less control over async behavior as when you invoke a delegate you can fetch the result using EndInvoke but you cannot terminate the process. While creating a thread you have more control on Async behavior. So in my understanding creating a Thread is better than invoking a delegate in Async way.
Implementation of BeginInvoke -













In the above example we saw how can we invoke a delegate using BeginInvoke.

So now in case GenerateMainXml has to return some value. How can we handle this situation ?
Answer is EndInvoke.
Calling EndInvoke will always block your main thread until it completes the execution. There are two ways of calling EndInvoke. One is as a callback and another one is direct way.
Direct way of calling an EndInvoke() -
 




So in the above example we created a Square number function and assigned it to a delegate. Called using BeginInvoke and on the same delegate called the EndInvoke method to get the results.
There is one problem in this approach. Use case of BeginInvoke is to get asynchronous way of implementing one of the method. But after calling EndInvoke we are telling to main thread to wait until delegates execution completes. Then only we can fetch the results .
So now what is the solution ?
Calling EndInvoke as Callback method. In this way we tell delegate once it completes the execution, call  the callback method and tell us the results. So main thread won't be blocked.








Looks Good :)

DynamicInvoke :  It is similar to Invoke, it invokes a delegate in asynchronous way. The only benefit is it takes  object[] as parameter. So it does boxing and unboxing at run time.







Correct implementation is -















So we can see in the above example we can pass an object array as aparameter in dynamicinvoke and the result will also be a object type. So this is usefule when you are not aware of the type but it will be damn slow. So think before using it.

Hope you like reading the article. Please inbox me in case of any confusion is in your mind.
Happy Coding:)

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

15 comments:

  1. Radha KarthiAugust 17, 2018 at 5:01 AM

    The way you have expressed your thoughts in this blog was nice with clear explanation. Thanks for sharing.
    dot net training institutes in chennai
    dotnet training in chennai

    ReplyDelete
    Replies
      Reply
  2. EnriqueSeptember 5, 2023 at 12:58 AM

    In programming, delegates are used to define and encapsulate methods, allowing them to be passed around as objects and executed at a later time. Sony Tv Using The Invoke method is a way to call a delegate.

    ReplyDelete
    Replies
      Reply
  3. unarıtm26September 28, 2023 at 3:41 AM

    Aksaray
    Aydın
    Kütahya
    Rize
    Bingöl
    ARW

    ReplyDelete
    Replies
      Reply
  4. RadiantHarbingerXSeptember 29, 2023 at 10:43 PM

    Diyarbakır
    Samsun
    Antep
    Kırşehir
    Konya
    PXPWNZ

    ReplyDelete
    Replies
      Reply
  5. 5GünerSeptember 30, 2023 at 6:01 PM

    Denizli
    Ankara
    Antep
    Bursa
    Eskişehir
    WQCR

    ReplyDelete
    Replies
      Reply
  6. CyberNomad42October 3, 2023 at 9:25 AM

    manisa
    sakarya
    sivas
    van
    elazığ
    HPF

    ReplyDelete
    Replies
      Reply
  7. YavuzOctober 5, 2023 at 6:08 AM

    sakarya
    kars
    gümüşhane
    elazığ
    manisa
    X367L6

    ReplyDelete
    Replies
      Reply
  8. PlasmaSeeker314October 7, 2023 at 4:20 PM

    görüntülüshow
    ücretli show
    FAL2T

    ReplyDelete
    Replies
      Reply
  9. PlasmaMatrixGlider13October 8, 2023 at 12:21 AM

    whatsapp goruntulu show
    show
    A4EO3

    ReplyDelete
    Replies
      Reply
  10. CB3B5Tara3A264November 9, 2023 at 10:09 AM

    77A48
    Bitcoin Nasıl Alınır
    Şırnak Parça Eşya Taşıma
    Karabük Evden Eve Nakliyat
    Erzincan Lojistik
    Yalova Parça Eşya Taşıma
    Çankaya Parke Ustası
    Ünye Parke Ustası
    Sivas Şehirler Arası Nakliyat
    Çorlu Lojistik

    ReplyDelete
    Replies
      Reply
  11. 465DELailah3FA2FNovember 10, 2023 at 10:41 AM

    EB0E8
    sarms
    Adana Evden Eve Nakliyat
    order sustanon
    buy clenbuterol
    peptides
    Malatya Evden Eve Nakliyat
    order steroids
    Maraş Evden Eve Nakliyat
    Van Evden Eve Nakliyat

    ReplyDelete
    Replies
      Reply
  12. BA3ABJosephB4592November 17, 2023 at 6:40 PM

    D23D4
    Silivri Boya Ustası
    Aksaray Evden Eve Nakliyat
    Çanakkale Evden Eve Nakliyat
    buy steroids
    Amasya Evden Eve Nakliyat
    Yalova Evden Eve Nakliyat
    testosterone propionat
    Giresun Evden Eve Nakliyat
    Düzce Evden Eve Nakliyat

    ReplyDelete
    Replies
      Reply
  13. 7BCCAJonathanD07CEDecember 5, 2023 at 1:06 PM

    6B1DF
    referans kodu

    ReplyDelete
    Replies
      Reply
  14. AnonymousFebruary 1, 2025 at 2:47 PM

    9B0A8D00A0
    aktif turk takipci

    ReplyDelete
    Replies
      Reply
  15. AnonymousFebruary 4, 2025 at 11:37 AM

    407D43F5D4
    kadın takipçi satın al
    Raid Promosyon Kodu
    Lords Mobile Promosyon Kodu
    Kaspersky Etkinleştirme Kodu
    Referans Kimliği Nedir
    Happn Promosyon Kodu
    Azar Elmas Kodu
    Kafa Topu Elmas Kodu
    Viking Rise Hediye Kodu

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

About The Author

Unknown
View my complete profile

Total Pageviews

84546

Popular Posts

  • Clr - Common Language Runtime
    .Net framework provides a run time environment - CLR. Common language runtime takes the IL code from the compiler( language specific) and p...
  • Auto logout chrome extension for Gmail
    Hello Friends, In the last article we learned to create a sample chrome extension. Here we are going to create auto logout Gmail script as...
  • Predicate delegate in C#
    Hello Everyone, In the article we will talk about Predicate delegate. Predicate is also a delegate which encapsulate a method that takes...
  • .Net Framework overview
    Hello friends : Here i am writing my first article on .Net framework anyways....So the question is What is .Net Framework ? The .Net fram...
  • Nagarro Placement Papers..
    Ques.1 :- Seat Reservation prog for the theatre. Write a function for seat allocation for the movie tickets. Total no of seats available are...
  • What does it mean by disconnected data access architecture of ADO.Net?
    ADO.Net introduces the concept of disconnected data architecture. In traditional data access components, you make a connection to the databa...
  • Calling the Delegates using Invoke(), BeginInvoke() and DynamicInvoke() ?
    Hello Guys, So in the last article we talked about What is delegate and how can we create a delegate. In this article we will discuss w...
  • C code to Check the string has valid identifier or not in.
    #include #include #include char keyword[][10]={"auto","break","case","char","const","...
  • Garbage Collection - Automatic Memory Management Part II
    Welcome friends in the second article of Garbage Collection. Those who have missed the first one can visit here . So in this article i will...
  • Delegates in C Sharp
    A Delegate is a type variable that holds the reference to a method. Delegates are similar to Pointer to functions in C and C++ When we...

Blog Archive

  • ►  2016 (4)
    • ►  September (2)
      • ►  Sep 03 (2)
    • ►  August (1)
      • ►  Aug 28 (1)
    • ►  April (1)
      • ►  Apr 24 (1)
  • ▼  2015 (12)
    • ▼  September (10)
      • ►  Sep 30 (1)
      • ►  Sep 29 (1)
      • ►  Sep 28 (1)
      • ►  Sep 27 (2)
      • ▼  Sep 26 (3)
        • Calling the Delegates using Invoke(), BeginInvoke(...
        • Delegates in C Sharp
        • Interfaces in C#
      • ►  Sep 20 (1)
      • ►  Sep 19 (1)
    • ►  August (1)
      • ►  Aug 16 (1)
    • ►  March (1)
      • ►  Mar 31 (1)
  • ►  2013 (10)
    • ►  June (1)
      • ►  Jun 16 (1)
    • ►  April (1)
      • ►  Apr 21 (1)
    • ►  February (8)
      • ►  Feb 18 (3)
      • ►  Feb 17 (2)
      • ►  Feb 16 (2)
      • ►  Feb 15 (1)
  • ►  2012 (1)
    • ►  May (1)
      • ►  May 27 (1)
  • ►  2010 (22)
    • ►  October (14)
      • ►  Oct 21 (1)
      • ►  Oct 06 (12)
      • ►  Oct 04 (1)
    • ►  April (2)
      • ►  Apr 22 (1)
      • ►  Apr 16 (1)
    • ►  March (1)
      • ►  Mar 30 (1)
    • ►  January (5)
      • ►  Jan 08 (3)
      • ►  Jan 01 (2)
  • ►  2009 (110)
    • ►  December (8)
      • ►  Dec 18 (2)
      • ►  Dec 05 (1)
      • ►  Dec 04 (5)
    • ►  November (1)
      • ►  Nov 27 (1)
    • ►  October (14)
      • ►  Oct 09 (4)
      • ►  Oct 07 (1)
      • ►  Oct 06 (3)
      • ►  Oct 05 (3)
      • ►  Oct 01 (3)
    • ►  September (17)
      • ►  Sep 30 (1)
      • ►  Sep 29 (1)
      • ►  Sep 28 (1)
      • ►  Sep 25 (1)
      • ►  Sep 24 (1)
      • ►  Sep 17 (2)
      • ►  Sep 15 (3)
      • ►  Sep 11 (2)
      • ►  Sep 09 (3)
      • ►  Sep 08 (2)
    • ►  August (31)
      • ►  Aug 31 (1)
      • ►  Aug 27 (3)
      • ►  Aug 26 (1)
      • ►  Aug 25 (2)
      • ►  Aug 24 (1)
      • ►  Aug 22 (2)
      • ►  Aug 21 (3)
      • ►  Aug 20 (2)
      • ►  Aug 19 (3)
      • ►  Aug 18 (1)
      • ►  Aug 16 (1)
      • ►  Aug 12 (2)
      • ►  Aug 11 (1)
      • ►  Aug 10 (3)
      • ►  Aug 07 (4)
      • ►  Aug 06 (1)
    • ►  July (24)
      • ►  Jul 25 (4)
      • ►  Jul 24 (20)
    • ►  April (15)
      • ►  Apr 10 (3)
      • ►  Apr 07 (9)
      • ►  Apr 06 (3)

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments
copyright @ TechGiant 2015. Powered by Blogger.

Disclaimer

This is my personal blog and i write articles on .Net, WPF, C#, OOPS, Threading and other .Net technologies. This is not related to any of my employer and organizations. This is the result of my personal interest.

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Followers

Copyright © 2025 A Developer Journey who codes for fun | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com