线程开启方式 【异步委托3种方式】

一、异步委托方式开启线程

    class Program
    {
        static void Main(string[] args) //在main方法钟执行,一个线程里面语句的执行顺序是从上往下的
        {
            //通过委托开启一个线程
            Func<string, string> a = x =>
             {
                 //让当前线程休眠100ms
                 Thread.Sleep(100);
                 return x;
             };
            //通过BeginInvoke开启一个新线程去执行a所引用的方法

            {
                //第一种写法
                //IAsyncResult取得当前线程的状态
                IAsyncResult ar = a.BeginInvoke("小明", null, null);
                //判断当前线程是否完毕
                if (ar.IsCompleted == false)
                {
                    Console.Write(" 未完成 ");
                    //控制当前线程对子线程的检测频率
                    Thread.Sleep(10);
                }
                var result = a.EndInvoke(ar);
                Console.WriteLine(result);
            }
            {
                //第二种写法
                //IAsyncResult取得当前线程的状态
                IAsyncResult ar = a.BeginInvoke("小明", null, null);
                //检测子线程是否结束 1000代表超时时间。如果等待了1000毫秒,线程还没有结束的话,方法返回false。如果在1000毫秒以内,线程结束了,方法返回true
                bool isEnd = ar.AsyncWaitHandle.WaitOne(1000);
                if (isEnd)
                {
                    var result = a.EndInvoke(ar);
                    Console.WriteLine(result);
                }
            }
            {
                //第三种写法
                //通过回调,检测线程结束,倒数第一个参数用来给回调函数传递数据 
                a.BeginInvoke(
                    "小明",
                    //线程执行完的回调
                    ar =>
                    {
                        //获取传入的data
                        string b = ar.AsyncState as String;
                        Console.WriteLine(b);
                        //获取执行结果
                        var result = a.EndInvoke(ar);
                        Console.WriteLine(result);
                    },
                    "AsyncState");
            }


            Console.WriteLine("Main");
            Console.ReadLine();
        }
    }

二、通过thread开启线程

    class Program
    {
        static void Main(string[] args) //在main方法钟执行,一个线程里面语句的执行顺序是从上往下的
        {
            //通过Thead开启一个线程,并且传递参数
            //第一种方式
            {
                Thread thread = new Thread((x) =>
                {
                    //获取当前线程Id
                    Console.WriteLine(x.ToString() + ",当前线程Id=" + Thread.CurrentThread.ManagedThreadId);
                });
                thread.Start("小明");
            }
            //第二种方式
            {
                Person person = new Person("小明");
                Thread thread = new Thread(person.Function);
                thread.Start();
            }
            
            Console.WriteLine("Main");
            Console.ReadLine();
        }
    }

    class Person
    {
        public string Name { get; set; }
        public Person(string name)
        {
            this.Name = name;
        }
        public void Function()
        {
            Console.WriteLine(Name);
        }
    }
posted @ 2022-03-31 14:34  InMyHeart,OnlyYou  阅读(146)  评论(0)    收藏  举报