C# 使用委托开始线程

使用委托的形式,调用线程,,,

using System;
using System.Threading;

namespace _012_线程
{
    class Program
    {
        static void Main(string[] args) //在mian中线程是执行一个线程里面的语句的执行,是从上到下的
        {
            //通过委托 开启一个线程
            //==============可用泛型传参数(无返回值)==============
            Action threaA = ThreadTestA;
            threaA.BeginInvoke(null,null);  //开启一个新的线程去执行,threaA所引用的方法


            Action<int> threaB = ThreadTestB;
            threaB.BeginInvoke(111,null, null);

            //可以认为线程是同时执行的 (异步执行)
            Console.WriteLine("异步执行");


            //================带返回值的形式====================

            //第一种方式  检测线程结束   ----- IsCompleted线程是否行完毕

            //Func<int, int> threaC = ThreadTestC;
            ////接收异步线程返回值
            //IAsyncResult returnResult = threaC.BeginInvoke(111, null, null);
            //while (!res.IsCompleted)
            //{
            //    Console.Write(".");
            //    Thread.Sleep(10); //控制子线程的检测频率,(每10ms检测一次)
            //}
            ////取得异步线程返回值
            //int result = threaC.EndInvoke(res);
            //Console.WriteLine("IsCompleted方式检测:" + result);


            //第二种方式  检测线程结束   -----   1000ms没结束就返回false,反之
            Func<int, int> threaC = ThreadTestC;
            //接收异步线程返回值
            IAsyncResult returnResult = threaC.BeginInvoke(111, null, null);
            bool isEnd = returnResult.AsyncWaitHandle.WaitOne(1000);
            int result = 0;
            if (isEnd)
            {
                result = threaC.EndInvoke(returnResult);
            }
            Console.WriteLine("EndInvoke()方式检测:" + isEnd +"  "+ result);

            //第三种方式   检测线程结束   -----  通过回调,检测线程结束

            Func<int,string, string> threaD = ThreadTestD;
            //倒数第二个参数,表示委托类型的参数,(回调函数)当线程结束的时候会调用这个委托指向的方法
            //最后一个参数,用来给回调函数传递数据
            IAsyncResult asy = threaD.BeginInvoke(111,"Czhenya", OnCallKey, threaD);

            //改为Lamdba表达式
            threaD.BeginInvoke(111, "Czhenya",(ar)=>{
                string res = threaD.EndInvoke(ar);
                Console.WriteLine("在Lamdba表达式中取得:"+res);
            },null);

            Console.ReadKey();
        }

        static void OnCallKey(IAsyncResult ar)
        {
            Func<int, string, string> thread = ar.AsyncState as Func<int, string, string>;
            string res = thread.EndInvoke(ar);

            Console.WriteLine("在回调函数中取到的结果 :"+res);
        }



        /// <summary>
        /// 一般是比较耗时的操作方法
        /// </summary>
        static void ThreadTestA()
        {
            Console.WriteLine("ThreaTestA");
        }
        static void ThreadTestB(int num)
        {
            Console.WriteLine("ThreaTestB "+num);
        }

        static int ThreadTestC(int num)
        {
            Console.WriteLine("ThreaTestC");
            Thread.Sleep(100);  //让当前线程休眠(暂停线程(参数单位:ms))
            return num;
        }

        static string ThreadTestD(int num,string str)
        {
            Console.WriteLine("ThreaTestD");
            return num +" "+ str;
        }
    }
}

运行结果图:

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈言必行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值