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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Selva GanapathyPosted Apr 10, 2014, 2:46 AM
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
Abhishek JaiswalPosted Apr 8, 2014, 10:48 AM
try this link:
http://stackoverflow.com/questions/2365718/running-multiple-delegates-asynchronously-and-waiting-for-response-in-c-sharp
:)
VulpesPosted Apr 8, 2014, 5:33 AM
Munesh SharmaPosted Apr 8, 2014, 12:12 AM