• C#之委托匿名


    public class DeleOrLamada
        {
            public void ThreadMessage(string data)
            {
                string message = string.Format("ThreadName is {0} Thread ID is {1}", data, Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine(message);
            }
            //模拟执行长时间任务
            public void Sleep(object state)
            {
                string name = (string)state;
                ThreadMessage(name);
                Console.WriteLine("我还没有执行完呢,请耐心等待....!!!");
                Thread.Sleep(3000);
            }
            //这里的参数不为object,你可以定义任何类型,任何参数
            public void ShowMessage(string message)
            {
                 ThreadMessage(message);
                 Console.WriteLine("我还没有执行完呢,请耐心等待....");
                 Thread.Sleep(3000);
             }
    }
        class Program
        {
            static void Main(string[] args)
            {
                DeleOrLamada deleOrLamada = new DeleOrLamada();
                Thread thread = new Thread(new ThreadStart(()=>{ deleOrLamada.ShowMessage("ShowMessage1"); }));      //又重新定义了一个函数,只是匿名了而已
                                                                                                                     //thread.Start();
               // ParameterizedThreadStart parameterizedThreadStart = new ParameterizedThreadStart(deleOrLamada.Sleep);   //直接绑定到Sleep函数
               // Thread thread1 = new Thread(parameterizedThreadStart);
               //注意这两种方法!!!!
                Thread thread1 = new Thread(new ParameterizedThreadStart((object a) => { deleOrLamada.Sleep("ShowMessage2"); }));
                thread1.Start();
                thread.Start();
                Console.ReadKey();
            }
        }
  • 相关阅读:
    (笔记)ubuntu中取消文件夹或文件等右下解一把锁的标志的方法
    (笔记)Linux常用命令大全
    (笔记)arm-linux-gcc/ld/objcopy/objdump参数总结
    (笔记)Ubuntu下安装arm-linux-gcc-4.4.3.tar.gz (交叉编译环境)
    (笔记)如何安装Arm-linux-gcc
    java application maven项目打自定义zip包
    几种简单的排序算法(JAVA)
    双色球机选算法java实现
    集合的子集输出(位运算方式)
    集合的子集输出(排列组合)
  • 原文地址:https://www.cnblogs.com/xingyuanzier/p/11153958.html
Copyright © 2020-2023  润新知