C# 使用委托开始线程

来源:互联网 发布:languages软件 编辑:程序博客网 时间:2024/06/05 10:30

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

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;        }    }}

运行结果图:

运行结果