详解C#基础之委托异步
2010-12-28 15:26
113 查看
大家知道委托就相当于C++里面的函数指针,相信大家都很很了解,看看如下简单代码来回想一下委托
- public delegate void Ad(); xu xus = new xu();
- Ad b = new Ad(xus.Add);
- b += xus.ex;
- b();
- Console.ReadLine();
- class xu
- {
- public void Add()
- {
- //Thread.Sleep(5000);
- Console.WriteLine("sssssssssssssssssssss");
- }
- public void ex()
- {
- //Thread.Sleep(5000);
- Console.WriteLine("aaaaaaaaaaaaaaaaaaaaa");
- } }
- public delegate int Ad(int x,int y);
- static void Main(string[] args)
- {
- xu xus = new xu();
- Ad a = new Ad(Add);
- a += (int x, int y) => { return x - y; };
- Console.WriteLine(a(3,2));
- Console.ReadLine();
- }
- static int Add(int x, int y)
- {
- //Thread.Sleep(2000);
- return x + y; }
- static int ex(int x, int y)
- {
- //Thread.Sleep(5000);
- return x - y;
- }
- }
- public delegate int Ad(int x,int y);
- static void Main(string[] args)
- {
- xu xus = new xu();
- Ad a = new Ad(Add);
- a += (int x, int y) => { return x - y; };
- Delegate[] d = a.GetInvocationList();
- for (int i = 0; i
- {
- if (d[i] is Ad)
- {
- Ad s = (Ad)d[i];
- Console.WriteLine(s(3, 2));
- }
- }
- Console.ReadLine();
- }
- static int Add(int x, int y)
- {
- // Thread.Sleep(2000);
- return x + y;
- }
- static int ex(int x, int y)
- {
- //Thread.Sleep(5000);
- return x - y;
- } }
- public delegate int Ad(int x,int y);
- static void Main(string[] args)
- {
- xu xus = new xu();
- Ad a = new Ad(Add);
- Console.WriteLine(a(3, 3));
- Console.WriteLine("start");
- Console.ReadLine();
- }static int Add(int x, int y)
- {
- Thread.Sleep(2000);
- return x + y;
- }
- public delegate int Ad(int x,int y);
- static void Main(string[] args)
- {
- xu xus = new xu();
- Ad a = new Ad(Add);
- Console.WriteLine(a(3, 3));
- // Console.WriteLine("start");
- IAsyncResult isa= a.BeginInvoke(3, 3, null, null);
- while (!isa.IsCompleted)
- {
- Console.WriteLine("未完成");
- }
- int s= a.EndInvoke(isa);
- Console.WriteLine(s.ToString());
- Console.ReadLine();
- }
- static int Add(int x, int y)
- {
- Thread.Sleep(2000);
- return x + y;
- }
- static int ex(int x, int y)
- {
- //Thread.Sleep(5000);
- return x - y;
- }
- public delegate int Ad(int x,int y);
- static void Main(string[] args)
- {
- xu xus = new xu();
- Ad a = new Ad(Add);
- Console.WriteLine(a(3, 3));
- IAsyncResult isa= a.BeginInvoke(3, 3, new AsyncCallback(call), "edit by xyl");
- //执行你想执行的代码 这里我们还是用IsCompleted来代替
- while (!isa.IsCompleted)
- {
- Console.WriteLine("未完成");
- }
- Console.ReadLine();
- }
- static void call(IAsyncResult isa)
- {
- AsyncResult ar = (AsyncResult)isa;
- Ad a = (Ad)ar.AsyncDelegate;
- Console.WriteLine("this is {0},{1}",a.EndInvoke(isa),ar.AsyncState);
- }
- static int Add(int x, int y)
- {
- Thread.Sleep(2000);
- return x + y;
- }
- static int ex(int x, int y)
- {
- //Thread.Sleep(5000);
- return x - y;
- } }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-682720/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-682720/
相关文章推荐
- C#基础之委托异步
- 一起谈.NET技术,C#基础之委托异步
- C#基础:线程之异步回调(委托)
- (转)C# 中的委托和事件(详解)2-委托和方法的异步调用
- 多线程和异步委托基础详解
- C#基础之“.NET研究”委托异步
- C#基础之--线程、任务和同步:一、异步委托
- C#基础:线程之异步委托
- 也来说说C#异步委托
- C#基础学习之委托
- C# 异步编程 异步委托调用同步方法
- C#实现异步编程的两个简单机制(异步委托&定时器)及Thread实现多线程
- C#基础教程(简单易懂)-神马是委托?
- C#委托的同步调用、异步调用、异步回调
- C#异步方法调用(四大方法详解)
- C#事件与委托详解【精华 多看看】
- C#委托的异步调用
- 【C#语言基础】--委托(Delegate)(一)
- 关于C#多线程之一——异步委托
- C#委托的异步调用【转】