一 调用多个方法
delegate
void
SampleDelegate(
string
content);
class Program
... {
static void Main(string[] args)
...{
SampleDelegate d1, d2, d3, d4, d5;
d1 = SayHello2;
d2 = SayHi2;
d3 = d1 + d2;
d4 = d3 - d1;
d5 = d1 + d1;
Console.WriteLine("Invoking delegate d1:");
d1("Daniel");
Console.WriteLine();
Console.WriteLine("Invoking delegate d2:");
d2("Daniel");
Console.WriteLine();
Console.WriteLine("Invoking delegate d3:");
d3("Daniel");
Console.WriteLine();
Console.WriteLine("Invoking delegate d4:");
d4("Daniel");
Console.WriteLine();
Console.WriteLine("Invoking delegate d5:");
d5("Daniel");
Console.WriteLine();
Console.ReadLine();
}
static void SayHello(string name)
...{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Hello, {0}", name);
}
static void SayHi(string name)
...{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Hi, {0}", name);
}
static void SayHello2(string name)
...{
Thread.Sleep(1000);
Console.WriteLine("Hello, {0}", name);
}
static void SayHi2(string name)
...{
Thread.Sleep(1000);
Console.WriteLine("Hi, {0}", name);
}
}
class Program
... {
static void Main(string[] args)
...{
SampleDelegate d1, d2, d3, d4, d5;
d1 = SayHello2;
d2 = SayHi2;
d3 = d1 + d2;
d4 = d3 - d1;
d5 = d1 + d1;
Console.WriteLine("Invoking delegate d1:");
d1("Daniel");
Console.WriteLine();
Console.WriteLine("Invoking delegate d2:");
d2("Daniel");
Console.WriteLine();
Console.WriteLine("Invoking delegate d3:");
d3("Daniel");
Console.WriteLine();
Console.WriteLine("Invoking delegate d4:");
d4("Daniel");
Console.WriteLine();
Console.WriteLine("Invoking delegate d5:");
d5("Daniel");
Console.WriteLine();
Console.ReadLine();
}
static void SayHello(string name)
...{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Hello, {0}", name);
}
static void SayHi(string name)
...{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Hi, {0}", name);
}
static void SayHello2(string name)
...{
Thread.Sleep(1000);
Console.WriteLine("Hello, {0}", name);
}
static void SayHi2(string name)
...{
Thread.Sleep(1000);
Console.WriteLine("Hi, {0}", name);
}
}
二 定义事件
class
Program
... {
static void Main(string[] args)
...{
Program prog = new Program();
SayHelloHandler handler = new SayHelloHandler(SayHello);
// 同步调用
Console.WriteLine("synchronous invoke begin");
Console.WriteLine(handler.Invoke("Daniel"));
Console.WriteLine("synchronous invoke end");
Console.WriteLine();
// 异步调用
Console.WriteLine("asynchronous invoke begin");
handler.BeginInvoke("Daniel", new AsyncCallback(callback), handler);
Console.WriteLine("asynchronous invoke end");
Console.ReadLine();
}
public static string SayHello(string name)
...{
Thread.Sleep(10000);
return string.Format("Hello World, {0}!", name);
}
public static void callback(IAsyncResult ar)
...{
SayHelloHandler handler = (SayHelloHandler)ar.AsyncState;
Console.WriteLine(handler.EndInvoke(ar));
}
}
delegate string SayHelloHandler( string name);
... {
static void Main(string[] args)
...{
Program prog = new Program();
SayHelloHandler handler = new SayHelloHandler(SayHello);
// 同步调用
Console.WriteLine("synchronous invoke begin");
Console.WriteLine(handler.Invoke("Daniel"));
Console.WriteLine("synchronous invoke end");
Console.WriteLine();
// 异步调用
Console.WriteLine("asynchronous invoke begin");
handler.BeginInvoke("Daniel", new AsyncCallback(callback), handler);
Console.WriteLine("asynchronous invoke end");
Console.ReadLine();
}
public static string SayHello(string name)
...{
Thread.Sleep(10000);
return string.Format("Hello World, {0}!", name);
}
public static void callback(IAsyncResult ar)
...{
SayHelloHandler handler = (SayHelloHandler)ar.AsyncState;
Console.WriteLine(handler.EndInvoke(ar));
}
}
delegate string SayHelloHandler( string name);