zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    Illegal access: this web application instance has been stopped already. could not load **
    mysql 分配内存大小配置
    top 命令
    Linux 创建用户 限制SFTP用户只能访问某个目录
    curl get请求
    VMware虚拟机ubuntu显示屏幕太小解决办法
    Micropython TPYBoard V10X拼插编程实践之定时器 代码不精通?...
    Micropython TPYBoard v102 自动浇花实验
    Micropython TPYBoard读取芯片上的温度传感器
    TPYBoard v102 DIY照相机(视频和制作流程)
  • 原文地址:https://www.cnblogs.com/xingyuanzier/p/11153958.html
Copyright © 2011-2022 走看看