MulticastDelegate Class
Represents a multicast delegate; that is, a delegate that can have more than one element in its invocation list.
Assembly: mscorlib (in mscorlib.dll)
MulticastDelegate is a special class. Compilers and other tools can derive from this class, but you cannot derive from it explicitly. The same is true of the Delegate class.
In addition to the methods that delegate types inherit from MulticastDelegate, the common language runtime provides two special methods: BeginInvoke and EndInvoke. For more information about these methods, see Calling Synchronous Methods Asynchronously.
A MulticastDelegate has a linked list of delegates, called an invocation list, consisting of one or more elements. When a multicast delegate is invoked, the delegates in the invocation list are called synchronously in the order in which they appear. If an error occurs during execution of the list then an exception is thrown.
The following example demonstrates the use of a class that is derived from MulticastDelegate.
Imports System Imports Microsoft.VisualBasic ' This class contains strings. It has a member method that ' accepts a multicast delegate as a parameter and calls it Class HoldsStrings ' The following line causes the compiler to generate ' a new delegate class named CheckAndPrintDelegate that ' inherits from System.MulticastDelegate. Delegate Sub CheckAndPrintDelegate(ByVal str As String) ' An ArrayList that holds strings Private myStringArray As New System.Collections.ArrayList() Public Sub addstring(ByVal str As String) myStringArray.Add(str) End Sub 'addstring ' Iterate through the strings and invoke the method(s) that the delegate points to Public Sub PrintAllQualified(ByVal myDelegate As CheckAndPrintDelegate) Dim str As String For Each str In myStringArray myDelegate(str) Next str End Sub 'PrintAllQualified End Class 'end of class HoldsStrings ' This class contains a few sample methods Class StringFuncs ' This method prints a string that it is passed if the string starts with a vowel Public Shared Sub ConStart(ByVal str As String) If Not (str.Chars(0) = "a"c Or str.Chars(0) = "e"c Or str.Chars(0) = "i"c Or str.Chars(0) = "o"c Or str.Chars(0) = "u"c) Then Console.WriteLine(str) End If End Sub 'ConStart ' This method prints a string that it is passed if the string starts with a consonant Public Shared Sub VowelStart(ByVal str As String) If (str.Chars(0) = "a"c Or str.Chars(0) = "e"c Or str.Chars(0) = "i"c Or str.Chars(0) = "o"c Or str.Chars(0) = "u"c) Then Console.WriteLine(str) End If End Sub 'VowelStart End Class 'StringFuncs ' This class demonstrates using Delegates, including using the Remove and ' Combine methods to create and modify delegate combinations. Class Test Public Shared Sub Main() ' Declare the HoldsStrings class and add some strings Dim myHoldsStrings As New HoldsStrings() myHoldsStrings.addstring("this") myHoldsStrings.addstring("is") myHoldsStrings.addstring("a") myHoldsStrings.addstring("multicast") myHoldsStrings.addstring("delegate") myHoldsStrings.addstring("example") ' Create two delegates individually using different methods Dim ConStartDel = New HoldsStrings.CheckAndPrintDelegate(AddressOf StringFuncs.ConStart) Dim VowStartDel As New HoldsStrings.CheckAndPrintDelegate(AddressOf StringFuncs.VowelStart) ' Demonstrate that MulticastDelegates may store only one delegate Dim DelegateList() As [Delegate] ' Returns an array of all delegates stored in the linked list of the ' MulticastDelegate. In these cases the lists will hold only one (Multicast) delegate DelegateList = ConStartDel.GetInvocationList() Console.WriteLine("ConStartDel contains " + DelegateList.Length.ToString() + " delegate(s).") DelegateList = VowStartDel.GetInvocationList() Console.WriteLine(("ConStartVow contains " + DelegateList.Length.ToString() + " delegate(s).")) ' Determine whether the delegates are System.Multicast delegates If TypeOf ConStartDel Is System.MulticastDelegate And TypeOf VowStartDel Is System.MulticastDelegate Then Console.WriteLine("ConStartDel and ConStartVow are System.MulticastDelegates") End If ' Run the two single delegates one after the other Console.WriteLine("Running ConStartDel delegate:") myHoldsStrings.PrintAllQualified(ConStartDel) Console.WriteLine("Running VowStartDel delegate:") myHoldsStrings.PrintAllQualified(VowStartDel) ' Create a new, empty MulticastDelegate Dim MultiDel As HoldsStrings.CheckAndPrintDelegate ' Delegate.Combine receives an unspecified number of MulticastDelegates as parameters MultiDel = CType([Delegate].Combine(ConStartDel, VowStartDel), HoldsStrings.CheckAndPrintDelegate) ' How many delegates is this delegate holding? DelegateList = MultiDel.GetInvocationList() Console.WriteLine((ControlChars.Cr + "MulitDel contains " + DelegateList.Length.ToString() + " delegates.")) ' What happens when this mulitcast delegate is passed to PrintAllQualified Console.WriteLine("Running the multiple delegate that combined the first two") myHoldsStrings.PrintAllQualified(MultiDel) ' The Remove and Combine methods modify the multiple delegate MultiDel = CType([Delegate].Remove(MultiDel, VowStartDel), HoldsStrings.CheckAndPrintDelegate) MultiDel = CType([Delegate].Combine(MultiDel, ConStartDel), HoldsStrings.CheckAndPrintDelegate) ' Finally, pass the combined delegates to PrintAllQualified again Console.WriteLine(ControlChars.Cr + "Running the multiple delegate that contains two copies of ConStartDel:") myHoldsStrings.PrintAllQualified(MultiDel) Return End Sub 'end of main End Class 'end of Test
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.