Hi,
How can I call two or more delegates at a single time?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Selva GanapathyRank #560 · 4.3K reputationPosted Apr 9, 2014, 10:46 PM EDT
http://www.c-sharpcorner.com/Blogs/14543/how-to-handle-delegate-and-multicast-delegates.aspx
public class MethodClass
{
public void Method1(string message)
{
System.Console.WriteLine("Method1 " + message);
}
public void Method2(string message)
{
System.Console.WriteLine("Method2 " + message);
}
}
class Program
{
public delegate void Del(string message);
static void Main(string[] args)
{
Del handler = DelegateMethod;
Console.WriteLine("calling a delegate method from current class");
// Call the delegate.
handler("Hello World");
Console.ReadLine();
MethodClass obj = new MethodClass();
Del d1 = obj.Method1;
Del d2 = obj.Method2;
Del d3 = DelegateMethod;
Del allMethodsDelegate = d1 + d2;
Console.WriteLine("calling a combined delegate method from other class");
allMethodsDelegate("Hello World");
allMethodsDelegate += d3;
Console.ReadLine();
Console.WriteLine("calling a combined delegate method from other class and current class");
allMethodsDelegate("Hello World");
Console.ReadLine();
int invocationCount = allMethodsDelegate.GetInvocationList().GetLength(0);
Console.WriteLine("Getting total count invocation list form a delegate, for the last one " + invocationCount.ToString() + " items (two form other class and one from current class ");
Console.ReadLine();
}
public static void DelegateMethod(string message)
{
Console.WriteLine(message);
}
public void MethodWithCallback(int param1, int param2, Del callback)
{
callback("The number is: " + (param1 + param2).ToString());
}
}
Regards,
Selva Ganapathy K
Munesh SharmaRank #174 · 13.9K reputationPosted Apr 7, 2014, 8:12 PM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 8, 2014, 1:33 AM EDT
Abhishek JaiswalRank #126 · 17.5K reputationPosted Apr 8, 2014, 6:48 AM EDT
try this link:
http://stackoverflow.com/questions/2365718/running-multiple-delegates-asynchronously-and-waiting-for-response-in-c-sharp
:)