using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Xml; using System.Threading; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Client application is starting!");
Thread.CurrentThread.Name = "Main Thread";
Cal cal = new Cal();
AddDelegate del = new AddDelegate(cal.Add);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 需要保存 IAsyncResult 对象以便后边条用EndInvoke方法时作参数传递</span> IAsyncResult asyncResult = del.BeginInvoke(<span style="color: #800080;">5</span>, <span style="color: #800080;">6</span>, <span style="color: #0000ff;">null</span>, <span style="color: #0000ff;">null</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 做某些其它的事情,模拟需要执行 3 秒钟</span> <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">1</span>; i <= <span style="color: #800080;">3</span>; i++<span style="color: #000000;">) { Thread.Sleep(TimeSpan.FromSeconds(i)); Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">{0}: Client executed {1} second(s).</span><span style="color: #800000;">"</span><span style="color: #000000;">, Thread.CurrentThread.Name, i); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 传递 IAsyncResult 对象</span> <span style="color: #0000ff;">int</span> rtnVal =<span style="color: #000000;"> del.EndInvoke(asyncResult); Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Result: {0} </span><span style="color: #800000;">"</span><span style="color: #000000;">, rtnVal); Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;"> Press any key to exit...</span><span style="color: #800000;">"</span><span style="color: #000000;">); Console.ReadLine(); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">delegate</span> <span style="color: #0000ff;">int</span> AddDelegate(<span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">int</span><span style="color: #000000;"> b); </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Cal { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Add(<span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">int</span><span style="color: #000000;"> b) { </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (Thread.CurrentThread.IsThreadPoolThread) { Thread.CurrentThread.Name </span>= <span style="color: #800000;">"</span><span style="color: #800000;">Pool Thread</span><span style="color: #800000;">"</span><span style="color: #000000;">; } Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Method invoked!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 做某些其它的事情,模拟需要执行 2 秒钟</span> <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">0</span>; i < <span style="color: #800080;">2</span>; i++<span style="color: #000000;">) { Thread.Sleep(TimeSpan.FromSeconds(</span><span style="color: #800080;">1</span><span style="color: #000000;">)); Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">{0}: Add executed {1} second(s).</span><span style="color: #800000;">"</span><span style="color: #000000;">, Thread.CurrentThread.Name, i); } Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Method complete!</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> a +<span style="color: #000000;"> b; } } }
}