码迷,mamicode.com
首页 > Windows程序 > 详细

C# 语法 ( 框架自带委托和异步 )

时间:2017-01-17 13:50:56      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:lin   pre   异步   size   ext   namespace   ace   sleep   callback   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace 异步线程
{
  class AsyncHronous
  {
    private int i = 0;

    public void Show()
    {
      for (int i = 0; i < 5; i++)
      {
        //系统自带的委托(有参数无返回值,有参数有返回值)
        Action action = new Action(Index);//回调函数(异步执行委托的第二个参数)
        AsyncCallback async = new AsyncCallback((x) => Console.WriteLine("我是回调值{0}", x.AsyncState));

        //异步执行委托(回调函数,回调函数的参数)
        action.BeginInvoke(async, 12);  
      }
    }

    public void Index()
    {
      Console.WriteLine("当前线程id是{0}", Thread.CurrentThread.ManagedThreadId);
      Thread.Sleep(1000);
      Console.WriteLine("我是第{0}名", i);
      i++;
    }
  }
}

 

<!--自带的委托-->

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace λ表达式
{
  public class LambdaA
  {
    public void show()
    {
      //有参数无返回值的系统自带委托(最多支持16个参数)
      Action<int, string, bool> action = (x, y, z) => Console.WriteLine(x + y + z);
      action(1, "2", true);

      //有参数有返回值的系统自带委托(最多支持16个参数和一个返回值)
      Func<int, int, int> func = (x, y) => x + y;
      Console.WriteLine(func(2, 3));
    }
  }
}

 

C# 语法 ( 框架自带委托和异步 )

标签:lin   pre   异步   size   ext   namespace   ace   sleep   callback   

原文地址:http://www.cnblogs.com/lovling/p/6292675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!