Click here to Skip to main content
13,459,076 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 21 Mar 2011

Multicast delegates in winform control update

, 21 Mar 2011
Rate this:
Please Sign up or sign in to vote.
People wonder how do delegates work and the threading issues associated with multicast ones.
Just to showcase multiple features of delegates in a single function :) This can be used to have a concise feature set of delegates

Problem statement: Using multicast delegates asynchronously to update a winform control using lambda expression

Problem elaboration

  • delegate
    • - that too multicast
      • async invocation
        • winform control update (threading issue)
          • lambda expression

Seems complicated ??

Not so much :) The example illustrates all the features available in delegate usage in .NET 3.5

public void DelegateTest ()
    // Create a multicast delegate
    Func<int, int> myDelegate;
    myDelegate = (i) => { return ++i; };
    myDelegate += (i) => { return i; };
    myDelegate += (j) => { return --j; };

    // Define a Action to update the textbox
    Action<int> textboxWrite = (delRes) => { textBox1.Text += "  " + delRes.ToString(); };

    // Iterate over delegates added in multicast delegate,
    // as a multicast delegate cannot be invoked asynchronously
    foreach (var d in myDelegate.GetInvocationList())
        // Cast the abstract delegate reference to our delegate type
        var myDel = d as Func<int, int>;

        // Invoke delegate asynchronously
            res =>
                // Get the result of invocation
                var result = myDel.EndInvoke(res);
                // Update the textbox by using BeginInvoke 
                // as control cannot be updated from non-owner thread
                textBox1.BeginInvoke(textboxWrite, new object[] { result });
            }, null);

    // That's all  ::)

Now the internals

  1. A delegate in .NET is multicast since it can hold references to multiple functions . These get added to the internally managed "InvocationList" which is a collection of delegates of the same type. Each function gets executed in sequence of adding.
    • Func and Action are specific delegates
      Func provides a return value
      Action does not provides a return value
      • Lambda expressions are 3.5 feature over previous anonymous functions (2.0 stuff)
        • Updating a winform control from non-owner thread requires a hold of the UI thread, using BeginInvoke. The UI thread is hyper performance intensive, so no other thread is given permission to mingle in its job.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nitin Singh India
Technical Lead 3PillarGlobal
India India
I am a solutions lead working on .NET for about 10 years, focusing on core framework implementations in project. My development interests are in middle tier and backend components and sometimes, identifying design elements for technical solutions. Apart, I collect coins and stamps and do photography whenever I get a chance.

You may also be interested in...


Comments and Discussions

GeneralGood Post Nitin Keep posting . Pin
ykg24-Mar-11 0:42
memberykg24-Mar-11 0:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180323.1 | Last Updated 21 Mar 2011
Article Copyright 2011 by Nitin Singh India
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid